Skip to content

Commit 3b7cc58

Browse files
committed
feat: analytics page event
1 parent 91864a9 commit 3b7cc58

File tree

8 files changed

+119
-2
lines changed

8 files changed

+119
-2
lines changed

apps/devmx/src/app/app.component.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Analytics } from '@devmx/shared-ui-global/analytics';
2+
import { Component, inject } from '@angular/core';
13
import { RouterOutlet } from '@angular/router';
2-
import { Component } from '@angular/core';
4+
import { env } from '../envs/env';
35

46
@Component({
57
selector: 'devmx-root',
@@ -13,4 +15,11 @@ import { Component } from '@angular/core';
1315
`,
1416
imports: [RouterOutlet],
1517
})
16-
export class AppComponent {}
18+
export class AppComponent {
19+
constructor() {
20+
if (env.prod) {
21+
const analytics = inject(Analytics);
22+
analytics.startTracking();
23+
}
24+
}
25+
}

apps/devmx/src/app/app.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy
22
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
33
import { provideEnv, provideHttpClientImpl } from '@devmx/shared-data-access';
44
import { providePresentation } from '@devmx/presentation-data-access';
5+
import { provideAnalytics } from '@devmx/shared-ui-global/analytics';
56
import { authInterceptor, loaderInterceptor } from './interceptors';
67
import { provideServiceWorker } from '@angular/service-worker';
78
import { provideAcademy } from '@devmx/academy-data-access';
@@ -84,6 +85,7 @@ export const appConfig: ApplicationConfig = {
8485
...provideAlbum(),
8586
...provideAcademy(),
8687
...provideLearn(),
88+
provideAnalytics(),
8789
provideServiceWorker('ngsw-worker.js', {
8890
enabled: !isDevMode(),
8991
registrationStrategy: 'registerWhenStable:30000',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @devmx/shared-ui-global/analytics
2+
3+
Secondary entry point of `@devmx/shared-ui-global`. It can be used by importing from `@devmx/shared-ui-global/analytics`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lib": {
3+
"entryFile": "src/index.ts"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/analytics'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Gtag, GtagConfig, GtagEvent, GtagEventParams } from './interfaces';
2+
import { NavigationEnd, Router } from '@angular/router';
3+
import { filter } from 'rxjs';
4+
5+
declare const gtag: Gtag;
6+
7+
export class Analytics {
8+
debug = false;
9+
10+
constructor(private router: Router) {}
11+
12+
startTracking() {
13+
this.router.events
14+
.pipe(filter((event) => event instanceof NavigationEnd))
15+
.subscribe((event) => {
16+
const page_location = event.urlAfterRedirects;
17+
this.event('page_view', { page_location, debug_mode: this.debug });
18+
});
19+
}
20+
21+
set(params: object) {
22+
gtag('set', params);
23+
}
24+
25+
event(event: GtagEvent, params?: GtagEventParams) {
26+
gtag('event', event, params);
27+
}
28+
29+
config(id: string, config?: GtagConfig) {
30+
gtag('config', id, config);
31+
}
32+
}
33+
34+
export function provideAnalytics() {
35+
return {
36+
provide: Analytics,
37+
deps: [Router],
38+
};
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export interface Gtag {
2+
<T extends 'set'>(command: T, params: object): void;
3+
<T extends 'config'>(command: T, id: string, config?: GtagConfig): void;
4+
<T extends 'event'>(
5+
command: T,
6+
event: GtagEvent,
7+
params?: GtagEventParams
8+
): void;
9+
}
10+
11+
export type GtagEvent =
12+
| 'page_view'
13+
| 'click'
14+
| 'purchase'
15+
| 'add_to_cart'
16+
| 'remove_from_cart'
17+
| 'begin_checkout'
18+
| 'sign_up'
19+
| 'login'
20+
| 'search'
21+
| 'view_item'
22+
| 'view_item_list'
23+
| 'share'
24+
| 'exception'
25+
| 'timing_complete';
26+
27+
export interface GtagConfig extends Record<string, unknown> {
28+
anonymize_ip?: boolean;
29+
page_path?: string;
30+
page_title?: string;
31+
user_id?: string;
32+
send_page_view?: boolean;
33+
allow_google_signals?: boolean;
34+
}
35+
36+
export interface GtagEventParams extends Record<string, unknown> {
37+
event_category?: string;
38+
event_label?: string;
39+
value?: number;
40+
page_title?: string;
41+
page_location?: string;
42+
non_interaction?: boolean;
43+
currency?: string;
44+
transaction_id?: string;
45+
debug_mode?: boolean;
46+
items?: GtagEventItem[];
47+
}
48+
49+
export interface GtagEventItem extends Record<string, unknown> {
50+
item_id?: string;
51+
item_name?: string;
52+
item_category?: string;
53+
price?: number;
54+
quantity?: number;
55+
}

tsconfig.base.json

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@
170170
"@devmx/shared-data-source": ["packages/shared/data-source/src/index.ts"],
171171
"@devmx/shared-resource": ["packages/shared/resource/src/index.ts"],
172172
"@devmx/shared-ui-global": ["packages/shared/ui-global/src/index.ts"],
173+
"@devmx/shared-ui-global/analytics": [
174+
"packages/shared/ui-global/analytics/src/index.ts"
175+
],
173176
"@devmx/shared-ui-global/bash": [
174177
"packages/shared/ui-global/bash/src/index.ts"
175178
],

0 commit comments

Comments
 (0)