@xylabs/pixel 1.4.10 → 1.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Pixel.js +42 -42
- package/dist/cjs/Pixel.js.map +1 -1
- package/dist/cjs/Referrer.js +8 -8
- package/dist/cjs/Referrer.js.map +1 -1
- package/dist/cjs/UniqueUserId.js +3 -3
- package/dist/cjs/UniqueUserId.js.map +1 -1
- package/dist/cjs/UtmFields.js +5 -6
- package/dist/cjs/UtmFields.js.map +1 -1
- package/dist/cjs/XyUserEventHandler.js +7 -7
- package/dist/cjs/XyUserEventHandler.js.map +1 -1
- package/dist/docs.json +285 -285
- package/dist/esm/Pixel.js +52 -52
- package/dist/esm/Pixel.js.map +1 -1
- package/dist/esm/Referrer.js +8 -8
- package/dist/esm/Referrer.js.map +1 -1
- package/dist/esm/UniqueUserId.js +3 -3
- package/dist/esm/UniqueUserId.js.map +1 -1
- package/dist/esm/UtmFields.js +4 -4
- package/dist/esm/UtmFields.js.map +1 -1
- package/dist/esm/XyUserEventHandler.js +5 -5
- package/dist/esm/XyUserEventHandler.js.map +1 -1
- package/dist/types/Api/UserEvent.d.ts +1 -1
- package/dist/types/Api/UserEvent.d.ts.map +1 -1
- package/dist/types/ExIds.d.ts +2 -2
- package/dist/types/ExIds.d.ts.map +1 -1
- package/dist/types/Pixel.d.ts +11 -11
- package/dist/types/Pixel.d.ts.map +1 -1
- package/dist/types/Referrer.d.ts +2 -2
- package/dist/types/Referrer.d.ts.map +1 -1
- package/dist/types/UniqueUserId.d.ts +1 -1
- package/dist/types/UniqueUserId.d.ts.map +1 -1
- package/dist/types/UserEventHandler.d.ts +2 -2
- package/dist/types/UserEventHandler.d.ts.map +1 -1
- package/dist/types/UtmFields.d.ts +1 -1
- package/dist/types/UtmFields.d.ts.map +1 -1
- package/dist/types/XyLabsTrackingEventJson.d.ts +3 -3
- package/dist/types/XyLabsTrackingEventJson.d.ts.map +1 -1
- package/dist/types/XyUserEventHandler.d.ts +2 -2
- package/dist/types/XyUserEventHandler.d.ts.map +1 -1
- package/package.json +19 -22
- package/src/Api/UserEvent.ts +1 -1
- package/src/ExIds.ts +2 -2
- package/src/Pixel.ts +55 -51
- package/src/Referrer.ts +9 -9
- package/src/UniqueUserId.ts +5 -3
- package/src/UserEventHandler.ts +2 -2
- package/src/UtmFields.ts +5 -5
- package/src/XyLabsTrackingEventJson.ts +3 -3
- package/src/XyUserEventHandler.ts +9 -5
package/src/Pixel.ts
CHANGED
|
@@ -13,12 +13,19 @@ import { UtmFields } from './UtmFields'
|
|
|
13
13
|
const emailHashLocalStorageName = 'xy_email_hash'
|
|
14
14
|
|
|
15
15
|
export class XyPixel {
|
|
16
|
-
public
|
|
16
|
+
public static api = new PixelApi()
|
|
17
|
+
|
|
18
|
+
private static _instance?: XyPixel
|
|
19
|
+
private static utmFieldsObj: UtmFields
|
|
20
|
+
|
|
21
|
+
public cid = new UniqueUserId().id
|
|
17
22
|
public email?: string
|
|
18
23
|
public email_hash?: string | null
|
|
19
|
-
public queue: UserEvent[] = []
|
|
20
|
-
public cid = new UniqueUserId().id
|
|
21
24
|
public exids?: ExIds
|
|
25
|
+
public pixelId?: string
|
|
26
|
+
|
|
27
|
+
public queue: UserEvent[] = []
|
|
28
|
+
|
|
22
29
|
private queueMutex = new Mutex()
|
|
23
30
|
|
|
24
31
|
private constructor(pixelId: string) {
|
|
@@ -26,49 +33,19 @@ export class XyPixel {
|
|
|
26
33
|
this.email_hash = localStorage.getItem(emailHashLocalStorageName)
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
public
|
|
30
|
-
this.
|
|
31
|
-
this.email_hash = email ? md5(email) : undefined
|
|
32
|
-
if (this.email_hash) {
|
|
33
|
-
localStorage.setItem(emailHashLocalStorageName, this.email_hash)
|
|
34
|
-
}
|
|
36
|
+
public static get instance(): XyPixel {
|
|
37
|
+
return assertEx(this._instance, 'XyPixel uninitialized')
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
fbc: Cookies.get('_fbc'),
|
|
41
|
-
fbp: Cookies.get('_fbp'),
|
|
42
|
-
ga: Cookies.get('_ga'),
|
|
43
|
-
gclid: Cookies.get('_gclid'),
|
|
44
|
-
rdt_uid: Cookies.get('rdt_uid'),
|
|
45
|
-
scid: Cookies.get('_scid'),
|
|
46
|
-
tt_sessionId: sessionStorage.getItem('tt_sessionId') ?? undefined,
|
|
47
|
-
},
|
|
48
|
-
}
|
|
40
|
+
public static init(pixelId: string) {
|
|
41
|
+
this._instance = new XyPixel(pixelId)
|
|
42
|
+
return this._instance
|
|
49
43
|
}
|
|
50
44
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (this.queue.length === 0) return
|
|
54
|
-
const api = XyPixel.api
|
|
55
|
-
if (api) {
|
|
56
|
-
const events = this.queue
|
|
57
|
-
this.queue = []
|
|
58
|
-
try {
|
|
59
|
-
await api.trackEvents(events)
|
|
60
|
-
} catch (ex) {
|
|
61
|
-
if (events) {
|
|
62
|
-
//put it back since it failed
|
|
63
|
-
this.queue = this.queue.concat(events)
|
|
64
|
-
}
|
|
65
|
-
console.error(ex)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
})
|
|
45
|
+
public static selectApi(api: PixelApi) {
|
|
46
|
+
this.api = api
|
|
69
47
|
}
|
|
70
48
|
|
|
71
|
-
private static utmFieldsObj: UtmFields
|
|
72
49
|
private static utmFields = () => {
|
|
73
50
|
if (XyPixel.utmFieldsObj === undefined) {
|
|
74
51
|
XyPixel.utmFieldsObj = new UtmFields()
|
|
@@ -76,6 +53,14 @@ export class XyPixel {
|
|
|
76
53
|
return XyPixel.utmFieldsObj
|
|
77
54
|
}
|
|
78
55
|
|
|
56
|
+
public identify(email?: string) {
|
|
57
|
+
this.email = email
|
|
58
|
+
this.email_hash = email ? md5(email) : undefined
|
|
59
|
+
if (this.email_hash) {
|
|
60
|
+
localStorage.setItem(emailHashLocalStorageName, this.email_hash)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
79
64
|
public async send<T extends Record<string, unknown>>(event: string, fields?: T, eventId?: string) {
|
|
80
65
|
this.updateFbId()
|
|
81
66
|
const utm = XyPixel.utmFields().update()
|
|
@@ -100,18 +85,37 @@ export class XyPixel {
|
|
|
100
85
|
await this.tryFlushQueue()
|
|
101
86
|
}
|
|
102
87
|
|
|
103
|
-
private
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
88
|
+
private async tryFlushQueue() {
|
|
89
|
+
await this.queueMutex.runExclusive(async () => {
|
|
90
|
+
if (this.queue.length === 0) return
|
|
91
|
+
const api = XyPixel.api
|
|
92
|
+
if (api) {
|
|
93
|
+
const events = this.queue
|
|
94
|
+
this.queue = []
|
|
95
|
+
try {
|
|
96
|
+
await api.trackEvents(events)
|
|
97
|
+
} catch (ex) {
|
|
98
|
+
if (events) {
|
|
99
|
+
//put it back since it failed
|
|
100
|
+
this.queue = this.queue.concat(events)
|
|
101
|
+
}
|
|
102
|
+
console.error(ex)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
})
|
|
111
106
|
}
|
|
112
107
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
108
|
+
private updateFbId() {
|
|
109
|
+
this.exids = {
|
|
110
|
+
...{
|
|
111
|
+
fbc: Cookies.get('_fbc'),
|
|
112
|
+
fbp: Cookies.get('_fbp'),
|
|
113
|
+
ga: Cookies.get('_ga'),
|
|
114
|
+
gclid: Cookies.get('_gclid'),
|
|
115
|
+
rdt_uid: Cookies.get('rdt_uid'),
|
|
116
|
+
scid: Cookies.get('_scid'),
|
|
117
|
+
tt_sessionId: sessionStorage.getItem('tt_sessionId') ?? undefined,
|
|
118
|
+
},
|
|
119
|
+
}
|
|
116
120
|
}
|
|
117
121
|
}
|
package/src/Referrer.ts
CHANGED
|
@@ -9,6 +9,15 @@ export class Referrer {
|
|
|
9
9
|
window.localStorage.setItem(Referrer.storageId, this.local)
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
public toJson() {
|
|
13
|
+
if ((this.local && this.local.length > 0) || (this.session && this.session.length > 0)) {
|
|
14
|
+
return {
|
|
15
|
+
local: this.local,
|
|
16
|
+
session: this.session,
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
12
21
|
private getFromLocal() {
|
|
13
22
|
const value = window.localStorage.getItem(Referrer.storageId)
|
|
14
23
|
if (value && value.length > 0) {
|
|
@@ -22,13 +31,4 @@ export class Referrer {
|
|
|
22
31
|
return value
|
|
23
32
|
}
|
|
24
33
|
}
|
|
25
|
-
|
|
26
|
-
public toJson() {
|
|
27
|
-
if ((this.local && this.local.length > 0) || (this.session && this.session.length > 0)) {
|
|
28
|
-
return {
|
|
29
|
-
local: this.local,
|
|
30
|
-
session: this.session,
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
34
|
}
|
package/src/UniqueUserId.ts
CHANGED
|
@@ -3,15 +3,17 @@ import randomBytes from 'randombytes'
|
|
|
3
3
|
export class UniqueUserId {
|
|
4
4
|
private static localStorageId = '_coin_cid'
|
|
5
5
|
public id: string
|
|
6
|
+
|
|
6
7
|
constructor() {
|
|
7
8
|
this.id = window.localStorage.getItem(UniqueUserId.localStorageId) ?? this.generateId()
|
|
8
9
|
window.localStorage.setItem(UniqueUserId.localStorageId, this.id)
|
|
9
10
|
}
|
|
10
|
-
private generateId() {
|
|
11
|
-
return randomBytes(16).toString('base64')
|
|
12
|
-
}
|
|
13
11
|
|
|
14
12
|
public toString() {
|
|
15
13
|
return this.id
|
|
16
14
|
}
|
|
15
|
+
|
|
16
|
+
private generateId() {
|
|
17
|
+
return randomBytes(16).toString('base64')
|
|
18
|
+
}
|
|
17
19
|
}
|
package/src/UserEventHandler.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { UserClickFields } from './UserClickFields'
|
|
|
4
4
|
import { ViewContentFields } from './ViewContentFields'
|
|
5
5
|
|
|
6
6
|
export abstract class UserEventHandler<T> {
|
|
7
|
-
abstract testStarted(fields: T | TestStartedFields): Promise<void>
|
|
8
7
|
abstract funnelStarted(fields: T | FunnelStartedFields): Promise<void>
|
|
9
|
-
abstract
|
|
8
|
+
abstract testStarted(fields: T | TestStartedFields): Promise<void>
|
|
10
9
|
abstract userClick(fields: T | UserClickFields): Promise<void>
|
|
10
|
+
abstract viewContent(fields: T | ViewContentFields): Promise<void>
|
|
11
11
|
}
|
package/src/UtmFields.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import isEqual from 'lodash
|
|
1
|
+
import { isEqual } from 'lodash-es'
|
|
2
2
|
|
|
3
3
|
export class UtmFields {
|
|
4
4
|
private static localStorageId = '_coin_utm'
|
|
@@ -31,6 +31,10 @@ export class UtmFields {
|
|
|
31
31
|
return Object.keys(record).length > 0 ? record : null
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
public toString() {
|
|
35
|
+
return JSON.stringify(this.fields)
|
|
36
|
+
}
|
|
37
|
+
|
|
34
38
|
//check the query string and if there an new/updated utm values, add them to the fields
|
|
35
39
|
public update() {
|
|
36
40
|
const record = this.getUtmRecord()
|
|
@@ -41,8 +45,4 @@ export class UtmFields {
|
|
|
41
45
|
}
|
|
42
46
|
return this.fields ?? undefined
|
|
43
47
|
}
|
|
44
|
-
|
|
45
|
-
public toString() {
|
|
46
|
-
return JSON.stringify(this.fields)
|
|
47
|
-
}
|
|
48
48
|
}
|
|
@@ -8,13 +8,13 @@ export interface XyLabsTrackingEventJson {
|
|
|
8
8
|
exids?: Record<string, string>
|
|
9
9
|
fields?: Record<string, unknown>
|
|
10
10
|
host?: string
|
|
11
|
+
ip?: string
|
|
11
12
|
pathname?: string
|
|
12
13
|
pixel?: string
|
|
13
14
|
receive_time?: number
|
|
14
15
|
rid?: string
|
|
15
|
-
uid?: string
|
|
16
|
-
utm?: Record<string, string>[] | Record<string, string[]>
|
|
17
16
|
system?: unknown
|
|
18
|
-
ip?: string
|
|
19
17
|
ua?: string
|
|
18
|
+
uid?: string
|
|
19
|
+
utm?: Record<string, string>[] | Record<string, string[]>
|
|
20
20
|
}
|
|
@@ -9,16 +9,20 @@ export class XyUserEventHandler<T extends Record<string, unknown>> extends UserE
|
|
|
9
9
|
constructor() {
|
|
10
10
|
super()
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
return await XyPixel.instance.send('TestStarted', fields)
|
|
14
|
-
}
|
|
12
|
+
|
|
15
13
|
public async funnelStarted(fields: T | FunnelStartedFields) {
|
|
16
14
|
return await XyPixel.instance.send('FunnelStarted', fields)
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
|
|
17
|
+
public async testStarted(fields: T | TestStartedFields) {
|
|
18
|
+
return await XyPixel.instance.send('TestStarted', fields)
|
|
20
19
|
}
|
|
20
|
+
|
|
21
21
|
public async userClick(fields: T | UserClickFields) {
|
|
22
22
|
return await XyPixel.instance.send('ViewPage', fields)
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
public async viewContent(fields: T | ViewContentFields) {
|
|
26
|
+
return await XyPixel.instance.send('ViewContent', fields)
|
|
27
|
+
}
|
|
24
28
|
}
|