-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
118 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { sanitizeUrl } from './analytics.service'; | ||
|
||
describe('AnalyticsService', () => { | ||
it('should redact the access token from URL', () => { | ||
const url = 'https://example.com/#access_token=123'; | ||
expect(sanitizeUrl(url)).toEqual('https://example.com/#access_token=redacted'); | ||
}); | ||
|
||
it('should redact the join key from URL', () => { | ||
const url = 'https://example.com/join/123'; | ||
expect(sanitizeUrl(url)).toEqual('https://example.com/join/redacted'); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
src/SIL.XForge.Scripture/ClientApp/src/xforge-common/analytics.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../environments/environment'; | ||
import { PwaService } from './pwa.service'; | ||
|
||
declare function gtag(...args: any): void; | ||
|
||
// Using a type rather than interface because I intend to turn in into a union type later for each type of event that | ||
// can be reported. | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
type EventParams = { | ||
page_path: string; | ||
}; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AnalyticsService { | ||
constructor(private readonly pwaService: PwaService) {} | ||
|
||
/** | ||
* Logs the page navigation event to the analytics service. This method is responsible for sanitizing the URL before | ||
* logging it. | ||
* @param url The URL of the page that was navigated to. | ||
*/ | ||
logNavigation(url: string): void { | ||
const sanitizedUrl = sanitizeUrl(url); | ||
this.logEvent('page_view', { page_path: sanitizedUrl }); | ||
} | ||
|
||
private logEvent(eventName: string, eventParams: EventParams): void { | ||
if (this.pwaService.isOnline && typeof environment.googleTagId === 'string') { | ||
gtag(eventName, environment.googleTagId, eventParams); | ||
} | ||
} | ||
} | ||
|
||
const redacted = 'redacted'; | ||
|
||
// redact access token from the hash | ||
function redactAccessToken(url: string): string { | ||
const urlObj = new URL(url); | ||
const hash = urlObj.hash; | ||
|
||
if (hash === '') return url; | ||
|
||
const hashObj = new URLSearchParams(hash.slice(1)); | ||
const accessToken = hashObj.get('access_token'); | ||
|
||
if (accessToken === null) return url; | ||
|
||
hashObj.set('access_token', redacted); | ||
urlObj.hash = hashObj.toString(); | ||
return urlObj.toString(); | ||
} | ||
|
||
function redactJoinKey(url: string): string { | ||
const urlObj = new URL(url); | ||
const pathParts = urlObj.pathname.split('/'); | ||
const joinIndex = pathParts.indexOf('join'); | ||
|
||
if (joinIndex === -1) { | ||
return url; | ||
} | ||
|
||
pathParts[joinIndex + 1] = redacted; | ||
urlObj.pathname = pathParts.join('/'); | ||
return urlObj.toString(); | ||
} | ||
|
||
/** | ||
* Redacts sensitive information from the given URL. Currently this only redacts the access token and the join key, so | ||
* if relying on this method in the future, be sure to check that it is still redacting everything you need it to. | ||
* @param url The URL to sanitize. | ||
* @returns A sanitized version of the URL. | ||
*/ | ||
export function sanitizeUrl(url: string): string { | ||
return redactAccessToken(redactJoinKey(url)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters