-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.ts
139 lines (128 loc) · 3.8 KB
/
event.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as metadata from "./metadata";
import session, { customSession, Session } from "./session";
import { track } from "./tracking";
import { version } from "../package.json";
import * as user from "./user";
import { window } from "./browser";
import dispatcher from "./dispatcher";
import * as partner from "./partner";
import { Campaign } from "./campaign";
const DOMMeta = (selector: HTMLElement | HTMLElement[] | string) =>
selector &&
(selector instanceof HTMLElement ||
selector[0] instanceof HTMLElement ||
typeof selector === "string")
? selector
: false;
export namespace AsaEvent {
export interface Event {
type: Type;
origin: string;
occurred: Date;
campaign?: Campaign;
tenant?: string;
user: {
did: string;
sid: string;
};
page: {
url?: string;
referrer?: string;
};
partner_id: string;
partner_sid: string;
meta: any;
v: string;
[data: string]: any;
}
export class Event {
constructor() {
const { id, referrer, campaign }: Session = session.getSession();
const partner_id: string = partner.getID();
const partner_sid: string = partner.getSID();
this.origin = window.location.origin;
this.occurred = new Date();
if (campaign) this.campaign = campaign;
this.user = {
did: user.getUser(),
sid: id
};
this.page = {
url: window.location.href
};
if (referrer) this.page.referrer = referrer;
if (dispatcher.id) this.tenant = dispatcher.id;
if (partner_id) this.partner_id = partner_id;
if (partner_sid) this.partner_sid = partner_sid;
this.v = version;
}
toJSON(): string {
return JSON.parse(JSON.stringify(Object.assign({}, this)));
}
[Symbol.toPrimitive](): string {
return this.type;
}
}
export namespace as.web {
export namespace order {
export class reviewed extends Event {
type = "as.web.order.reviewed";
}
}
export namespace customer.account {
export class provided extends Event {
type = "as.web.customer.account.provided";
}
}
export namespace product {
export class product extends Event {
constructor(data) {
super();
if (DOMMeta(data)) {
const meta = metadata.extract(data);
if (meta) this.meta = meta;
} else {
this.meta = { ...data, ...metadata.extractFromHead() };
}
}
}
export namespace availability {
export class checked extends product {
type = "as.web.product.availability.checked";
}
}
export class carted extends product {
type = "as.web.product.carted";
}
export class searched extends product {
type = "as.web.product.searched";
}
export namespace shipping {
export class selected extends product {
type = "as.web.product.shipping.selected";
}
}
export class viewed extends product {
type = "as.web.product.viewed";
}
}
export namespace payment {
export class completed extends Event {
type = "as.web.payment.completed";
}
}
}
export const web: {
[name: string]: new (...args: any[]) => Event;
} = {
"as.web.customer.account.provided": as.web.customer.account.provided,
"as.web.order.reviewed": as.web.order.reviewed,
"as.web.product.availability.checked": as.web.product.availability.checked,
"as.web.product.carted": as.web.product.carted,
"as.web.product.searched": as.web.product.searched,
"as.web.product.shipping.selected": as.web.product.shipping.selected,
"as.web.product.viewed": as.web.product.viewed,
"as.web.payment.completed": as.web.payment.completed
};
export type Type = keyof typeof web;
}