@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.
@@ -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 { InAppEvents, JourneysEvents, newEvent, allEvents, gistToCIO, ContentType } from './events'
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
- siteId: string | undefined
12
- events: EventListenerOrEventListenerObject | null | undefined
18
+ siteId: string | undefined
19
+ events: EventListenerOrEventListenerObject | null | undefined
13
20
 
14
- _env: string | undefined
15
- _logging: boolean | undefined
21
+ _env: string | undefined
22
+ _logging: boolean | undefined
16
23
 
17
- anonymousInApp: boolean | false
24
+ anonymousInApp: boolean | false
18
25
  }
19
26
 
20
- export function InAppPlugin(
21
- settings: InAppPluginSettings,
22
- ): Plugin {
23
-
24
- let _analytics: Analytics;
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
- Gist.events.on('messageShown', (message: any) => {
50
- const deliveryId:string = message?.properties?.gist?.campaignId;
51
- if (settings.events) {
52
- _eventTarget.dispatchEvent(newEvent(InAppEvents.MessageOpened, {
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
- async function page(ctx: Context): Promise<Context> {
136
- if(!_pluginLoaded)
137
- return ctx;
138
-
139
- const page:string = ctx.event?.properties?.name ?? ctx.event?.properties?.url;
140
- if(typeof page === 'string' && page.length > 0) {
141
- Gist.setCurrentRoute(page);
142
- }
143
-
144
- return ctx;
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
- async function reset(ctx: Context): Promise<Context> {
148
- await Gist.clearUserToken();
149
- return ctx;
150
- }
151
-
152
- async function syncUserToken(ctx: Context): Promise<Context> {
153
- if(!_gistLoaded)
154
- return ctx;
155
-
156
- const user = _analytics.user().id();
157
- if (typeof user === 'string' && user.length > 0) {
158
- await Gist.setUserToken(user);
159
- } else {
160
- await Gist.clearUserToken();
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
- return ctx;
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
- const customerio: Plugin = {
166
- name: 'Customer.io In-App Plugin',
167
- type: 'before',
168
- version: '0.0.1',
169
- isLoaded: (): boolean => _pluginLoaded,
170
- load: async (ctx: Context, instance: Analytics) => {
171
- _analytics = instance;
172
-
173
- if(settings.siteId == null || settings.siteId == "") {
174
- _error("siteId is required. Can't initialize.")
175
- return ctx;
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
- return customerio;
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
- console.error(`[Customer.io In-App Plugin] ${msg}`)
209
- }
240
+ console.error(`[Customer.io In-App Plugin] ${msg}`)
241
+ }