Skip to content

Commit f5b0169

Browse files
committed
* Include additional share URL to test redact
* Await online status before sending requests * Moved GA configuration to Analytics Service
1 parent 0c589c1 commit f5b0169

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/SIL.XForge.Scripture/ClientApp/src/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SVKBDV7K3Q"></script>
4+
<script async src="https://www.googletagmanager.com/gtag/js"></script>
55
<script>
66
window.dataLayer = window.dataLayer || [];
77
function gtag() {
88
dataLayer.push(arguments);
99
}
10-
gtag("js", new Date());
11-
12-
gtag("config", "G-SVKBDV7K3Q");
1310
</script>
1411

1512
<link href="https://fonts.googleapis.com/icon?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet" />

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ describe('AnalyticsService', () => {
77
});
88

99
it('should redact the join key from URL', () => {
10-
const url = 'https://example.com/join/123';
11-
expect(sanitizeUrl(url)).toEqual('https://example.com/join/redacted');
10+
['https://example.com/join/123', 'https://example.com/join/123/en'].forEach(url => {
11+
expect(sanitizeUrl(url)).toContain('https://example.com/join/redacted');
12+
});
1213
});
1314
});

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,39 @@ import { OnlineStatusService } from './online-status.service';
44

55
declare function gtag(...args: any): void;
66

7-
// Using a type rather than interface because I intend to turn in into a union type later for each type of event that
8-
// can be reported.
9-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
10-
type EventParams = {
11-
page_path: string;
12-
};
7+
interface CommandParams {}
8+
9+
enum GoogleCommands {
10+
Config = 'config',
11+
Event = 'event',
12+
JavaScript = 'js'
13+
}
14+
15+
interface ConfigParams extends CommandParams {
16+
send_page_view?: boolean;
17+
}
18+
19+
interface PageViewParams extends CommandParams {
20+
page_location?: string;
21+
page_title?: string;
22+
}
1323

1424
@Injectable({ providedIn: 'root' })
1525
export class AnalyticsService {
16-
constructor(private readonly onlineStatus: OnlineStatusService) {}
26+
private initiated?: Promise<void>;
27+
constructor(private readonly onlineStatus: OnlineStatusService) {
28+
if (typeof environment.googleTagId !== 'string') {
29+
return;
30+
}
31+
32+
this.initiated = new Promise(resolve => {
33+
this.onlineStatus.online.then(() => {
34+
this.send(GoogleCommands.JavaScript, new Date());
35+
this.send(GoogleCommands.Config, environment.googleTagId, { send_page_view: false } as ConfigParams);
36+
resolve();
37+
});
38+
});
39+
}
1740

1841
/**
1942
* Logs the page navigation event to the analytics service. This method is responsible for sanitizing the URL before
@@ -22,13 +45,11 @@ export class AnalyticsService {
2245
*/
2346
logNavigation(url: string): void {
2447
const sanitizedUrl = sanitizeUrl(url);
25-
this.logEvent('page_view', { page_path: sanitizedUrl });
48+
this.send(GoogleCommands.Event, 'page_view', { page_location: sanitizedUrl } as PageViewParams);
2649
}
2750

28-
private logEvent(eventName: string, eventParams: EventParams): void {
29-
if (this.onlineStatus.isOnline && typeof environment.googleTagId === 'string') {
30-
gtag(eventName, environment.googleTagId, eventParams);
31-
}
51+
private send(command: GoogleCommands, name: any, params?: CommandParams): void {
52+
Promise.all([this.initiated, this.onlineStatus.online]).then(() => gtag(command, name, params));
3253
}
3354
}
3455

0 commit comments

Comments
 (0)