@customerio/cdp-analytics-browser 0.3.4 → 0.3.6
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/generated/version.js +1 -1
- package/dist/cjs/plugins/in-app-plugin/index.js +74 -56
- package/dist/cjs/plugins/in-app-plugin/index.js.map +1 -1
- package/dist/pkg/generated/version.js +1 -1
- package/dist/pkg/plugins/in-app-plugin/index.js +75 -57
- package/dist/pkg/plugins/in-app-plugin/index.js.map +1 -1
- package/dist/types/core/buffer/index.d.ts +1 -1
- package/dist/types/generated/version.d.ts +1 -1
- package/dist/types/plugins/in-app-plugin/index.d.ts.map +1 -1
- package/dist/umd/646.js +1 -1
- package/dist/umd/646.js.gz +0 -0
- package/dist/umd/inAppPlugin.js +1 -1
- package/dist/umd/inAppPlugin.js.gz +0 -0
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.gz +0 -0
- package/dist/umd/standalone.js +1 -1
- package/dist/umd/standalone.js.gz +0 -0
- package/package.json +3 -3
- package/src/generated/version.ts +1 -1
- package/src/plugins/in-app-plugin/index.ts +217 -185
|
@@ -2,208 +2,240 @@ import { Analytics } from '../../core/analytics'
|
|
|
2
2
|
import { Context } from '../../core/context'
|
|
3
3
|
import { Plugin } from '../../core/plugin'
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
InAppEvents,
|
|
7
|
+
JourneysEvents,
|
|
8
|
+
newEvent,
|
|
9
|
+
allEvents,
|
|
10
|
+
gistToCIO,
|
|
11
|
+
ContentType,
|
|
12
|
+
} from './events'
|
|
6
13
|
import Gist from 'customerio-gist-web'
|
|
7
14
|
|
|
8
15
|
export { InAppEvents }
|
|
9
16
|
|
|
10
17
|
export type InAppPluginSettings = {
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
siteId: string | undefined
|
|
19
|
+
events: EventListenerOrEventListenerObject | null | undefined
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
_env: string | undefined
|
|
22
|
+
_logging: boolean | undefined
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
anonymousInApp: boolean | false
|
|
18
25
|
}
|
|
19
26
|
|
|
20
|
-
export function InAppPlugin(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let _gistLoaded:boolean = false;
|
|
26
|
-
let _pluginLoaded:boolean = false;
|
|
27
|
-
const _eventTarget:EventTarget = new EventTarget();
|
|
28
|
-
|
|
29
|
-
function attachListeners() {
|
|
30
|
-
if(!_gistLoaded || _pluginLoaded)
|
|
31
|
-
return;
|
|
32
|
-
|
|
33
|
-
_analytics.on('reset', reset);
|
|
34
|
-
|
|
35
|
-
if(settings.events) {
|
|
36
|
-
allEvents.forEach((event) => {
|
|
37
|
-
_eventTarget.addEventListener(event, settings?.events as EventListenerOrEventListenerObject);
|
|
38
|
-
});
|
|
39
|
-
['messageDismissed', 'messageError'].forEach((event) => {
|
|
40
|
-
Gist.events.on(event, (message: any) => {
|
|
41
|
-
_eventTarget.dispatchEvent(newEvent(gistToCIO(event), {
|
|
42
|
-
messageId: message.messageId,
|
|
43
|
-
deliveryId: message.properties?.gist?.campaignId,
|
|
44
|
-
}));
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
27
|
+
export function InAppPlugin(settings: InAppPluginSettings): Plugin {
|
|
28
|
+
let _analytics: Analytics
|
|
29
|
+
let _gistLoaded = false
|
|
30
|
+
let _pluginLoaded = false
|
|
31
|
+
const _eventTarget: EventTarget = new EventTarget()
|
|
48
32
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
messageId: message?.messageId,
|
|
54
|
-
deliveryId: deliveryId,
|
|
55
|
-
message: {
|
|
56
|
-
dismiss: function() {
|
|
57
|
-
Gist.dismissMessage(message?.instanceId);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}));
|
|
61
|
-
}
|
|
62
|
-
if (typeof deliveryId != 'undefined' && deliveryId != '') {
|
|
63
|
-
_analytics.track(JourneysEvents.Metric, {
|
|
64
|
-
'deliveryId': deliveryId,
|
|
65
|
-
'metric': JourneysEvents.Opened,
|
|
66
|
-
});
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const broadcastId:Number = message?.properties?.gist?.broadcast?.broadcastIdInt;
|
|
70
|
-
if (broadcastId) {
|
|
71
|
-
const templateId = message?.properties?.gist?.broadcast?.templateId;
|
|
72
|
-
_analytics.track(JourneysEvents.Content, {
|
|
73
|
-
'actionType': JourneysEvents.ViewedContent,
|
|
74
|
-
'contentId': broadcastId,
|
|
75
|
-
'templateId': templateId,
|
|
76
|
-
'contentType': ContentType,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
Gist.events.on('messageAction', (params: any) => {
|
|
82
|
-
const deliveryId:string = params?.message?.properties?.gist?.campaignId;
|
|
83
|
-
if (settings.events) {
|
|
84
|
-
_eventTarget.dispatchEvent(newEvent(InAppEvents.MessageAction, {
|
|
85
|
-
messageId: params.message.messageId,
|
|
86
|
-
deliveryId: deliveryId,
|
|
87
|
-
action: params.action,
|
|
88
|
-
name: params.name,
|
|
89
|
-
actionName: params.name,
|
|
90
|
-
actionValue: params.action,
|
|
91
|
-
message: {
|
|
92
|
-
dismiss: function() {
|
|
93
|
-
Gist.dismissMessage(params.message.instanceId);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
if (params.action == 'gist://close') {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (typeof deliveryId != 'undefined' && deliveryId != '') {
|
|
102
|
-
_analytics.track(JourneysEvents.Metric, {
|
|
103
|
-
'deliveryId': deliveryId,
|
|
104
|
-
'metric': JourneysEvents.Clicked,
|
|
105
|
-
'actionName': params.name,
|
|
106
|
-
'actionValue': params.action,
|
|
107
|
-
});
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
const broadcastId:Number = params?.message?.properties?.gist?.broadcast?.broadcastIdInt;
|
|
111
|
-
if (broadcastId) {
|
|
112
|
-
const templateId:Number = params?.message?.properties?.gist?.broadcast?.templateId;
|
|
113
|
-
_analytics.track(JourneysEvents.Content, {
|
|
114
|
-
'actionType': JourneysEvents.ClickedContent,
|
|
115
|
-
'contentId': broadcastId,
|
|
116
|
-
'templateId': templateId,
|
|
117
|
-
'contentType': ContentType,
|
|
118
|
-
'actionName': params.name,
|
|
119
|
-
'actionValue': params.action,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
Gist.events.on('eventDispatched', (gistEvent: any) => {
|
|
125
|
-
if(gistEvent.name == 'analytics:track') {
|
|
126
|
-
const trackEventName:string = gistEvent.payload?.event;
|
|
127
|
-
if(typeof trackEventName === 'undefined' || trackEventName == '') {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
_analytics.track(trackEventName, gistEvent.payload?.properties, gistEvent.payload?.options);
|
|
131
|
-
}
|
|
132
|
-
});
|
|
33
|
+
async function setAnonymousId() {
|
|
34
|
+
const anonymousId = _analytics.user().anonymousId()
|
|
35
|
+
if (anonymousId) {
|
|
36
|
+
await Gist.setCustomAttribute('cio_anonymous_id', anonymousId)
|
|
133
37
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function attachListeners() {
|
|
41
|
+
if (!_gistLoaded || _pluginLoaded) return
|
|
42
|
+
|
|
43
|
+
_analytics.on('reset', reset)
|
|
44
|
+
|
|
45
|
+
if (settings.events) {
|
|
46
|
+
allEvents.forEach((event) => {
|
|
47
|
+
_eventTarget.addEventListener(
|
|
48
|
+
event,
|
|
49
|
+
settings?.events as EventListenerOrEventListenerObject
|
|
50
|
+
)
|
|
51
|
+
})
|
|
52
|
+
;['messageDismissed', 'messageError'].forEach((event) => {
|
|
53
|
+
Gist.events.on(event, (message: any) => {
|
|
54
|
+
_eventTarget.dispatchEvent(
|
|
55
|
+
newEvent(gistToCIO(event), {
|
|
56
|
+
messageId: message.messageId,
|
|
57
|
+
deliveryId: message.properties?.gist?.campaignId,
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
145
62
|
}
|
|
146
63
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
64
|
+
Gist.events.on('messageShown', (message: any) => {
|
|
65
|
+
const deliveryId: string = message?.properties?.gist?.campaignId
|
|
66
|
+
if (settings.events) {
|
|
67
|
+
_eventTarget.dispatchEvent(
|
|
68
|
+
newEvent(InAppEvents.MessageOpened, {
|
|
69
|
+
messageId: message?.messageId,
|
|
70
|
+
deliveryId: deliveryId,
|
|
71
|
+
message: {
|
|
72
|
+
dismiss: function () {
|
|
73
|
+
Gist.dismissMessage(message?.instanceId)
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
if (typeof deliveryId !== 'undefined' && deliveryId !== '') {
|
|
80
|
+
void _analytics.track(JourneysEvents.Metric, {
|
|
81
|
+
deliveryId: deliveryId,
|
|
82
|
+
metric: JourneysEvents.Opened,
|
|
83
|
+
})
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
const broadcastId: Number =
|
|
87
|
+
message?.properties?.gist?.broadcast?.broadcastIdInt
|
|
88
|
+
if (broadcastId) {
|
|
89
|
+
const templateId = message?.properties?.gist?.broadcast?.templateId
|
|
90
|
+
void _analytics.track(JourneysEvents.Content, {
|
|
91
|
+
actionType: JourneysEvents.ViewedContent,
|
|
92
|
+
contentId: broadcastId,
|
|
93
|
+
templateId: templateId,
|
|
94
|
+
contentType: ContentType,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
Gist.events.on('messageAction', (params: any) => {
|
|
100
|
+
const deliveryId: string = params?.message?.properties?.gist?.campaignId
|
|
101
|
+
if (settings.events) {
|
|
102
|
+
_eventTarget.dispatchEvent(
|
|
103
|
+
newEvent(InAppEvents.MessageAction, {
|
|
104
|
+
messageId: params.message.messageId,
|
|
105
|
+
deliveryId: deliveryId,
|
|
106
|
+
action: params.action,
|
|
107
|
+
name: params.name,
|
|
108
|
+
actionName: params.name,
|
|
109
|
+
actionValue: params.action,
|
|
110
|
+
message: {
|
|
111
|
+
dismiss: function () {
|
|
112
|
+
Gist.dismissMessage(params.message.instanceId)
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
if (params.action === 'gist://close') {
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
if (typeof deliveryId !== 'undefined' && deliveryId !== '') {
|
|
122
|
+
void _analytics.track(JourneysEvents.Metric, {
|
|
123
|
+
deliveryId: deliveryId,
|
|
124
|
+
metric: JourneysEvents.Clicked,
|
|
125
|
+
actionName: params.name,
|
|
126
|
+
actionValue: params.action,
|
|
127
|
+
})
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
const broadcastId: Number =
|
|
131
|
+
params?.message?.properties?.gist?.broadcast?.broadcastIdInt
|
|
132
|
+
if (broadcastId) {
|
|
133
|
+
const templateId: Number =
|
|
134
|
+
params?.message?.properties?.gist?.broadcast?.templateId
|
|
135
|
+
void _analytics.track(JourneysEvents.Content, {
|
|
136
|
+
actionType: JourneysEvents.ClickedContent,
|
|
137
|
+
contentId: broadcastId,
|
|
138
|
+
templateId: templateId,
|
|
139
|
+
contentType: ContentType,
|
|
140
|
+
actionName: params.name,
|
|
141
|
+
actionValue: params.action,
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
Gist.events.on('eventDispatched', (gistEvent: any) => {
|
|
147
|
+
if (gistEvent.name === 'analytics:track') {
|
|
148
|
+
const trackEventName: string = gistEvent.payload?.event
|
|
149
|
+
if (typeof trackEventName === 'undefined' || trackEventName === '') {
|
|
150
|
+
return
|
|
161
151
|
}
|
|
162
|
-
|
|
152
|
+
void _analytics.track(
|
|
153
|
+
trackEventName,
|
|
154
|
+
gistEvent.payload?.properties,
|
|
155
|
+
gistEvent.payload?.options
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function page(ctx: Context): Context {
|
|
162
|
+
if (!_pluginLoaded) return ctx
|
|
163
|
+
|
|
164
|
+
const page: string =
|
|
165
|
+
ctx.event?.properties?.name ?? ctx.event?.properties?.url
|
|
166
|
+
if (typeof page === 'string' && page.length > 0) {
|
|
167
|
+
Gist.setCurrentRoute(page)
|
|
163
168
|
}
|
|
164
169
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
await Gist.setup({
|
|
179
|
-
siteId: settings.siteId,
|
|
180
|
-
env: settings._env? settings._env : "prod",
|
|
181
|
-
logging: settings._logging,
|
|
182
|
-
useAnonymousSession: settings.anonymousInApp,
|
|
183
|
-
});
|
|
184
|
-
_gistLoaded = true;
|
|
185
|
-
|
|
186
|
-
await syncUserToken(ctx);
|
|
187
|
-
attachListeners();
|
|
188
|
-
|
|
189
|
-
_pluginLoaded = true;
|
|
190
|
-
|
|
191
|
-
return Promise.resolve();
|
|
192
|
-
},
|
|
193
|
-
identify: syncUserToken,
|
|
194
|
-
page: page,
|
|
195
|
-
unload: async () => {
|
|
196
|
-
if(settings.events) {
|
|
197
|
-
allEvents.forEach((event) => {
|
|
198
|
-
_eventTarget.removeEventListener(event, settings?.events as EventListenerOrEventListenerObject);
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
|
-
}
|
|
170
|
+
return ctx
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function reset(ctx: Context): Promise<Context> {
|
|
174
|
+
await Gist.clearUserToken()
|
|
175
|
+
await Gist.clearCustomAttributes()
|
|
176
|
+
await setAnonymousId()
|
|
177
|
+
return ctx
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function syncUserToken(ctx: Context): Promise<Context> {
|
|
181
|
+
if (!_gistLoaded) return ctx
|
|
203
182
|
|
|
204
|
-
|
|
183
|
+
const user = _analytics.user().id()
|
|
184
|
+
if (typeof user === 'string' && user.length > 0) {
|
|
185
|
+
await Gist.setUserToken(user)
|
|
186
|
+
} else {
|
|
187
|
+
await Gist.clearUserToken()
|
|
188
|
+
}
|
|
189
|
+
return ctx
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const customerio: Plugin = {
|
|
193
|
+
name: 'Customer.io In-App Plugin',
|
|
194
|
+
type: 'before',
|
|
195
|
+
version: '0.0.1',
|
|
196
|
+
isLoaded: (): boolean => _pluginLoaded,
|
|
197
|
+
load: async (ctx: Context, instance: Analytics) => {
|
|
198
|
+
_analytics = instance
|
|
199
|
+
|
|
200
|
+
if (settings.siteId == null || settings.siteId === '') {
|
|
201
|
+
_error("siteId is required. Can't initialize.")
|
|
202
|
+
return ctx
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
await setAnonymousId()
|
|
206
|
+
|
|
207
|
+
await Gist.setup({
|
|
208
|
+
siteId: settings.siteId,
|
|
209
|
+
env: settings._env ? settings._env : 'prod',
|
|
210
|
+
logging: settings._logging,
|
|
211
|
+
useAnonymousSession: settings.anonymousInApp,
|
|
212
|
+
})
|
|
213
|
+
_gistLoaded = true
|
|
214
|
+
|
|
215
|
+
await syncUserToken(ctx)
|
|
216
|
+
attachListeners()
|
|
217
|
+
|
|
218
|
+
_pluginLoaded = true
|
|
219
|
+
|
|
220
|
+
return Promise.resolve()
|
|
221
|
+
},
|
|
222
|
+
identify: syncUserToken,
|
|
223
|
+
page: page,
|
|
224
|
+
unload: () => {
|
|
225
|
+
if (settings.events) {
|
|
226
|
+
allEvents.forEach((event) => {
|
|
227
|
+
_eventTarget.removeEventListener(
|
|
228
|
+
event,
|
|
229
|
+
settings?.events as EventListenerOrEventListenerObject
|
|
230
|
+
)
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return customerio
|
|
205
237
|
}
|
|
206
238
|
|
|
207
239
|
function _error(msg: string) {
|
|
208
|
-
|
|
209
|
-
}
|
|
240
|
+
console.error(`[Customer.io In-App Plugin] ${msg}`)
|
|
241
|
+
}
|