-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from center-for-threat-informed-defense/TIE-78…
…_install_analytics TIE-78: Install Google Analytics
- Loading branch information
Showing
11 changed files
with
268 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,14 @@ | |
<link href="https://fonts.googleapis.com/css2?family=Oswald:[email protected]&display=swap" rel="stylesheet"> | ||
<link href="https://fonts.googleapis.com/css2?family=Libre+Franklin:ital,wght@0,100..900;1,100..900&display=swap" | ||
rel="stylesheet"> | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NLXWXCSGXF"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { dataLayer.push(arguments); } | ||
gtag('js', new Date()); | ||
|
||
gtag('config', 'G-NLXWXCSGXF', { 'anonymize_ip': true }); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
|
125 changes: 125 additions & 0 deletions
125
src/tie-web-interface/src/assets/scripts/Application/EventRecorder.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,125 @@ | ||
import { SetViewFilter } from "../PredictionsView/Commands/SetViewFilter"; | ||
import { SetViewLimit } from "../PredictionsView/Commands/SetViewLimit"; | ||
import { SetViewOption } from "../PredictionsView/Commands/SetViewOption"; | ||
import type { EventStorage } from "./EventStorage"; | ||
import type { ControlCommand } from "../PredictionsView"; | ||
|
||
export class EventRecorder { | ||
|
||
/** | ||
* The recorder's event store. | ||
*/ | ||
private _storage: EventStorage; | ||
|
||
|
||
/** | ||
* Creates a new {@link EventRecorder}. | ||
* @param storage | ||
* The recorder's event store. | ||
*/ | ||
constructor(storage: EventStorage) { | ||
this._storage = storage; | ||
} | ||
|
||
|
||
/** | ||
* Records an "add techniques" event. | ||
* @param method | ||
* The method used to add the techniques. | ||
* @param techniques | ||
* The techniques added. | ||
*/ | ||
public addTechniques(method: string, techniques: string[]) { | ||
// Sanitize techniques | ||
const sanitizedTechniques = techniques | ||
.filter(id => id.match(/^T[0-9]{4}(?:.[0-9]{3})?$/gi)) | ||
.map(id => id.toLocaleUpperCase()); | ||
// Record | ||
this._storage.record( | ||
"add_techniques", | ||
{ | ||
"method": method, | ||
"techniques": sanitizedTechniques, | ||
"total_techniques": sanitizedTechniques.length | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Records a "technique prediction" event. | ||
* @param techniqueBasis | ||
* The observed techniques provided for the prediction. | ||
* @param backend | ||
* The prediction backend. | ||
* @param time | ||
* The prediction time (in ms). | ||
*/ | ||
public makePrediction(techniques: string[], backend: string, time: number,) { | ||
this._storage.record( | ||
"make_prediction", | ||
{ | ||
"observed_techniques": techniques, | ||
"total_observed_techniques": techniques.length, | ||
"prediction_backend": backend, | ||
"prediction_time": time | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Records an "apply view control" event. | ||
* @param cmd | ||
* The applied control command. | ||
*/ | ||
public applyViewControl(cmd: ControlCommand) { | ||
// If view filter... | ||
if (cmd instanceof SetViewFilter) { | ||
this._storage.record( | ||
"apply_filter_control", | ||
{ | ||
"control": cmd.control.name, | ||
"filter": cmd.filter ?? "all_filters", | ||
"value": cmd.value | ||
} | ||
); | ||
return; | ||
} | ||
// If view option... | ||
if (cmd instanceof SetViewOption) { | ||
this._storage.record( | ||
"apply_organization_control", | ||
{ | ||
"control": cmd.control.name, | ||
"value": cmd.value | ||
} | ||
); | ||
return; | ||
} | ||
// If view limit... | ||
if (cmd instanceof SetViewLimit) { | ||
this._storage.record( | ||
"apply_view_limit_control", | ||
{ | ||
"control": cmd.control.name, | ||
"value": cmd.value | ||
} | ||
) | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Records a "download file" event. | ||
* @param fileType | ||
* The file's type. | ||
*/ | ||
public downloadArtifact(fileType: string) { | ||
this._storage.record( | ||
"download_file", | ||
{ | ||
"file_type": fileType | ||
} | ||
) | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/tie-web-interface/src/assets/scripts/Application/EventStorage.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,14 @@ | ||
import type { RecordParameter } from "./RecordParameter"; | ||
|
||
export interface EventStorage { | ||
|
||
/** | ||
* Records an event to the event store. | ||
* @param name | ||
* The event's name. | ||
* @param parameters | ||
* The event's parameters. | ||
*/ | ||
record(name: string, parameters: { [key: string]: RecordParameter; }): void; | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/tie-web-interface/src/assets/scripts/Application/GoogleEventStorage.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,41 @@ | ||
import type { EventStorage } from "./EventStorage"; | ||
import type { RecordParameter } from "./RecordParameter"; | ||
|
||
declare const gtag: ( | ||
command: "event", | ||
event_name: string, | ||
parameters: { [key: string]: RecordParameter; } | ||
) => void | ||
|
||
export class GoogleEventStorage implements EventStorage { | ||
|
||
/** | ||
* Creates a new {@link GoogleEventStorage}. | ||
*/ | ||
constructor() { } | ||
|
||
/** | ||
* Records an event to the event store. | ||
* @remarks | ||
* Please ensure the event `name` follows Google Analytic's naming convention of | ||
* `[verb]_[noun]` where the verb is in the simple present tense. For example: | ||
* - `join_group` | ||
* - `view_item` | ||
* - `select_item` | ||
* - `spend_virtual_currency` | ||
* @param name | ||
* The event's name. | ||
* @param parameters | ||
* The event's parameters. | ||
*/ | ||
record(name: string, parameters: { [key: string]: RecordParameter; }): void { | ||
// Validate record | ||
if (!name.match(/^[a-z][a-z_]*$/g)) { | ||
const error = `Event name '${name}' does not follow typical convention.`; | ||
throw new Error(error); | ||
} | ||
// Record event | ||
gtag('event', name, parameters); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
src/tie-web-interface/src/assets/scripts/Application/RecordParameter.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 @@ | ||
export type RecordParameter = string | number | boolean | string[] | number[] | boolean[]; |
4 changes: 4 additions & 0 deletions
4
src/tie-web-interface/src/assets/scripts/Application/index.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 |
---|---|---|
@@ -1 +1,5 @@ | ||
export * from "./AppConfiguration"; | ||
export * from "./EventRecorder"; | ||
export * from "./EventStorage"; | ||
export * from "./GoogleEventStorage"; | ||
export * from "./RecordParameter"; |
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
Oops, something went wrong.