UNPKG

1.57 kBJavaScriptView Raw
1export class Serializable {
2
3 constructor(props) {
4 this.properties = props || {};
5 }
6
7 toObject() {
8 return this.properties;
9 }
10
11 toString() {
12 return JSON.stringify(this.toObject());
13 }
14
15 toJSON() {
16 return JSON.stringify(this.properties);
17 }
18
19 toQueryString() {
20 var str = [];
21 var obj = this.toObject();
22 for (var p in obj) {
23 if (obj.hasOwnProperty(p) && obj[p]) {
24 str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
25 }
26 }
27 return str.join('&');
28 }
29}
30
31class Hit extends Serializable {
32
33 sent = false
34
35 constructor(props){
36 super(props);
37 }
38}
39
40
41export class PageHit extends Hit {
42 constructor(screenName, screenTitle) {
43 super({ dt: screenName, dp: screenTitle || screenName, t: 'pageview' });
44 }
45}
46
47export class ScreenHit extends Hit {
48 constructor(screenName) {
49 super({ cd: screenName, t: 'screenview' });
50 }
51}
52
53export class Event extends Hit {
54 constructor(category, action, label, value) {
55 super({ ec: category, ea: action, el: label, ev: value, t: 'event' });
56 }
57}
58
59export class Transaction extends Hit {
60 constructor(id, affiliation, revenue, shipping, tax) {
61 super({ ti: id, ta: affiliation, tr: revenue, tt: tax, t: 'transaction' });
62 }
63}
64
65export class AddItem extends Hit {
66 constructor(id, name, price, quantity, sku, category) {
67 super({ti: id, in: name, ip: price, iq: quantity, ic: sku, iv: category, t: 'item' });
68 }
69}