-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathincendium.ts
86 lines (74 loc) · 1.94 KB
/
incendium.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { Angulartics2 } from '../../angulartics2-core';
enum EventNames {
RUN = 'run',
GO = 'go',
}
export enum IncendiumEventNames {
ADD_CONVERION = 'add_conversion',
}
interface IIncendiumResponse {
value: string;
type: IncendiumEventNames;
}
type TIncendiumParams =
| {
key: string;
}
| boolean;
declare var inc: (e: EventNames | IncendiumEventNames, v?: TIncendiumParams, g?: boolean) => string;
declare global {
interface Window {
INCENDIUM: any;
}
}
@Injectable({ providedIn: 'root' })
export class Angulartics2Incendium {
incendiumResponse = new Subject<Partial<IIncendiumResponse>>();
constructor(private angulartics2: Angulartics2) {
if (typeof inc === 'undefined') {
console.warn('Angulartics 2 Incendium Plugin: incendium global not found');
}
}
startTracking(): void {
this.angulartics2.pageTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(x => this.pageTrack(x.path));
this.angulartics2.eventTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(({ action, properties }) => this.trackAction(action, properties));
}
/**
* Track Page in Incendium
*
* @param path location
*/
pageTrack(path: string) {
inc(EventNames.RUN, false, true);
}
/**
* Track Action
*
* @param action Action name
* @param properties params
*/
async trackAction(action: string, properties: any) {
try {
switch (action as IncendiumEventNames) {
case IncendiumEventNames.ADD_CONVERION:
inc(IncendiumEventNames.ADD_CONVERION, properties.key);
break;
default:
break;
}
const res = await inc(EventNames.GO);
this.incendiumResponse.next({
value: res,
type: IncendiumEventNames.ADD_CONVERION,
});
} catch (error) {
this.incendiumResponse.error(error);
}
}
}