1
- /* */
2
1
// Utils.
3
2
import './utils/polyfills' ;
4
3
import { uuid , getLanguage } from './utils' ;
@@ -19,7 +18,8 @@ const {
19
18
Data,
20
19
} = Altis . Analytics ;
21
20
22
- if ( ! Config . PinpointId || ! Config . CognitoId ) {
21
+ if ( ! Config . PinpointId || ! Config . CognitoId ) {
22
+ /* eslint-disable quotes */
23
23
console . warn (
24
24
"Altis Analytics: Missing configuration. \
25
25
You must define the following constants in PHP:\n \
@@ -29,6 +29,7 @@ if ( !Config.PinpointId || !Config.CognitoId ) {
29
29
define( 'ALTIS_ANALYTICS_COGNITO_REGION', '...' ); \
30
30
"
31
31
) ;
32
+ /* eslint-enable quotes */
32
33
}
33
34
34
35
/**
@@ -66,7 +67,7 @@ const params = new URLSearchParams( window.location.search );
66
67
const utm = {
67
68
utm_source : params . get ( 'utm_source' ) || '' ,
68
69
utm_medium : params . get ( 'utm_medium' ) || '' ,
69
- utm_campaign : params . get ( 'utm_campaign' ) || ''
70
+ utm_campaign : params . get ( 'utm_campaign' ) || '' ,
70
71
} ;
71
72
72
73
/**
@@ -91,7 +92,10 @@ const getSessionID = () => {
91
92
} ;
92
93
const getSearchParams = ( ) =>
93
94
Array . from ( new URLSearchParams ( window . location . search ) . entries ( ) ) . reduce (
94
- ( carry , [ name , value ] ) => ( { [ `qv_${ name } ` ] : value , ...carry } ) ,
95
+ ( carry , [ name , value ] ) => ( {
96
+ [ `qv_${ name } ` ] : value ,
97
+ ...carry ,
98
+ } ) ,
95
99
{ }
96
100
) ;
97
101
const getAttributes = ( extra = { } ) =>
@@ -131,7 +135,7 @@ const overwriteMerge = ( destinationArray, sourceArray ) => sourceArray;
131
135
const Analytics = {
132
136
keys : {
133
137
UserId : `aws.cognito.identity-id.${ Config . CognitoId } ` ,
134
- UserCredentials : `aws.cognito.identity-credentials.${ Config . CognitoId } `
138
+ UserCredentials : `aws.cognito.identity-credentials.${ Config . CognitoId } ` ,
135
139
} ,
136
140
getUserId : ( ) => localStorage . getItem ( Analytics . keys . UserId ) ,
137
141
getUserCredentials : ( ) => {
@@ -140,8 +144,10 @@ const Analytics = {
140
144
if ( new Date ( ParsedCredentials . Credentials . Expiration ) . getTime ( ) > Date . now ( ) ) {
141
145
return ParsedCredentials ;
142
146
}
143
- } catch ( error ) { }
144
- return false ;
147
+ return false ;
148
+ } catch ( error ) {
149
+ return false ;
150
+ }
145
151
} ,
146
152
setUserId : id => localStorage . setItem ( Analytics . keys . UserId , id ) ,
147
153
setUserCredentials : credentials => localStorage . setItem ( Analytics . keys . UserCredentials , JSON . stringify ( credentials ) ) ,
@@ -156,15 +162,15 @@ const Analytics = {
156
162
// Configure Cognito client.
157
163
const params = {
158
164
region : Config . CognitoRegion ,
159
- credentials : { }
165
+ credentials : { } ,
160
166
} ;
161
167
if ( Config . CognitoEndpoint ) {
162
168
params . endpoint = Config . CognitoEndpoint ;
163
169
}
164
170
const client = new CognitoIdentityClient ( params ) ;
165
171
166
172
// Get unique ID if not set.
167
- if ( ! UserId ) {
173
+ if ( ! UserId ) {
168
174
try {
169
175
const getIdCommand = new GetIdCommand ( { IdentityPoolId : Config . CognitoId } ) ;
170
176
const result = await client . send ( getIdCommand ) ;
@@ -196,8 +202,8 @@ const Analytics = {
196
202
Analytics . client = ( async ( ) => {
197
203
// Get user credentials for pinpoint client.
198
204
const Credentials = await Analytics . authenticate ( ) ;
199
- if ( ! Credentials ) {
200
- console . error ( 'Credentials not found.' , error ) ;
205
+ if ( ! Credentials ) {
206
+ console . error ( 'Credentials not found.' ) ;
201
207
return ;
202
208
}
203
209
@@ -208,8 +214,8 @@ const Analytics = {
208
214
accessKeyId : Credentials . AccessKeyId ,
209
215
secretAccessKey : Credentials . SecretKey ,
210
216
expiration : Credentials . Expiration ,
211
- sessionToken : Credentials . SessionToken
212
- }
217
+ sessionToken : Credentials . SessionToken ,
218
+ } ,
213
219
} ;
214
220
if ( Config . PinpointEndpoint ) {
215
221
params . endpoint = Config . PinpointEndpoint ;
@@ -226,13 +232,13 @@ const Analytics = {
226
232
} ,
227
233
getEndpoint : ( ) => {
228
234
try {
229
- const ParsedEndpoint = JSON . parse ( localStorage . getItem ( ` aws.pinpoint.endpoint` ) ) ;
235
+ const ParsedEndpoint = JSON . parse ( localStorage . getItem ( ' aws.pinpoint.endpoint' ) ) ;
230
236
return ParsedEndpoint || { } ;
231
237
} catch ( error ) {
232
238
return { } ;
233
- } ;
239
+ }
234
240
} ,
235
- setEndpoint : ( endpoint ) => localStorage . setItem ( ` aws.pinpoint.endpoint` , JSON . stringify ( endpoint ) ) ,
241
+ setEndpoint : endpoint => localStorage . setItem ( ' aws.pinpoint.endpoint' , JSON . stringify ( endpoint ) ) ,
236
242
mergeEndpointData : ( endpoint = { } ) => {
237
243
const Existing = Analytics . getEndpoint ( ) ;
238
244
const UAData = UAParser ( navigator . userAgent ) ;
@@ -276,7 +282,7 @@ const Analytics = {
276
282
277
283
// Merge new endpoint data with defaults.
278
284
endpoint = merge . all ( [ EndpointData , Existing , endpoint ] , {
279
- arrayMerge : overwriteMerge
285
+ arrayMerge : overwriteMerge ,
280
286
} ) ;
281
287
282
288
// Store the endpoint data.
@@ -309,9 +315,9 @@ const Analytics = {
309
315
Metrics : Object . assign ( { } , data . metrics || { } ) ,
310
316
Session : {
311
317
Id : subSessionId /* required */ ,
312
- StartTimestamp : new Date ( subSessionStart ) . toISOString ( ) /* required */
313
- }
314
- }
318
+ StartTimestamp : new Date ( subSessionStart ) . toISOString ( ) , /* required */
319
+ } ,
320
+ } ,
315
321
} ;
316
322
317
323
// Add session stop parameters.
@@ -324,7 +330,7 @@ const Analytics = {
324
330
Analytics . events . push ( Event ) ;
325
331
326
332
// Flush the events if we don't want to queue.
327
- if ( ! queue ) {
333
+ if ( ! queue ) {
328
334
Analytics . flushEvents ( ) ;
329
335
return ;
330
336
}
@@ -342,7 +348,7 @@ const Analytics = {
342
348
343
349
// Events are associated with an endpoint.
344
350
const UserId = Analytics . getUserId ( ) ;
345
- if ( ! UserId ) {
351
+ if ( ! UserId ) {
346
352
console . error ( 'No User ID found. Make sure to call Analytics.authenticate() first.' ) ;
347
353
return ;
348
354
}
@@ -357,23 +363,26 @@ const Analytics = {
357
363
Endpoint . RequestId = uuid ( ) ;
358
364
359
365
// Reduce events to an object keyed by event ID.
360
- const Events = Analytics . events . reduce ( ( carry , event ) => ( { ...event , ...carry } ) , { } ) ;
366
+ const Events = Analytics . events . reduce ( ( carry , event ) => ( {
367
+ ...event ,
368
+ ...carry ,
369
+ } ) , { } ) ;
361
370
362
371
// Build events request object.
363
372
const BatchUserId = UserId . replace ( `${ Config . CognitoRegion } :` , '' ) ;
364
373
const EventsRequest = {
365
374
BatchItem : {
366
375
[ BatchUserId ] : {
367
376
Endpoint : Endpoint ,
368
- Events : Events
369
- }
370
- }
377
+ Events : Events ,
378
+ } ,
379
+ } ,
371
380
} ;
372
381
373
382
try {
374
383
const command = new PutEventsCommand ( {
375
384
ApplicationId : Config . PinpointId ,
376
- EventsRequest : EventsRequest
385
+ EventsRequest : EventsRequest ,
377
386
} ) ;
378
387
const result = await client . send ( command ) ;
379
388
@@ -384,7 +393,7 @@ const Analytics = {
384
393
} catch ( error ) {
385
394
console . error ( error ) ;
386
395
}
387
- }
396
+ } ,
388
397
} ;
389
398
390
399
// Set initial endpoint data.
@@ -398,7 +407,7 @@ document.addEventListener( 'visibilitychange', () => {
398
407
// Fire session stop event.
399
408
Analytics . record ( '_session.stop' , {
400
409
attributes : getAttributes ( { } ) ,
401
- metrics : getMetrics ( { } )
410
+ metrics : getMetrics ( { } ) ,
402
411
} ) ;
403
412
} else {
404
413
// On show reset start time.
@@ -408,7 +417,7 @@ document.addEventListener( 'visibilitychange', () => {
408
417
subSessionStart = Date . now ( ) ;
409
418
// Fire session start event.
410
419
Analytics . record ( '_session.start' , {
411
- attributes : getAttributes ( { } )
420
+ attributes : getAttributes ( { } ) ,
412
421
} ) ;
413
422
}
414
423
} ) ;
@@ -417,13 +426,13 @@ document.addEventListener( 'visibilitychange', () => {
417
426
window . addEventListener ( 'DOMContentLoaded' , ( ) => {
418
427
// Session start.
419
428
Analytics . record ( '_session.start' , {
420
- attributes : getAttributes ( )
429
+ attributes : getAttributes ( ) ,
421
430
} ) ;
422
431
// Record page view event & create/update endpoint immediately.
423
432
Analytics . record (
424
433
'pageView' ,
425
434
{
426
- attributes : getAttributes ( )
435
+ attributes : getAttributes ( ) ,
427
436
} ,
428
437
{ } ,
429
438
false
@@ -434,7 +443,7 @@ window.addEventListener( 'DOMContentLoaded', () => {
434
443
window . addEventListener ( 'beforeunload' , async ( ) => {
435
444
Analytics . record ( '_session.stop' , {
436
445
attributes : getAttributes ( { } ) ,
437
- metrics : getMetrics ( { } )
446
+ metrics : getMetrics ( { } ) ,
438
447
} ) ;
439
448
await Analytics . flushEvents ( ) ;
440
449
} ) ;
@@ -446,7 +455,7 @@ window.Altis.Analytics.record = ( type, data = {}, endpoint = {} ) =>
446
455
type ,
447
456
{
448
457
attributes : getAttributes ( data . attributes || { } ) ,
449
- metrics : getMetrics ( data . metrics || { } )
458
+ metrics : getMetrics ( data . metrics || { } ) ,
450
459
} ,
451
460
endpoint
452
461
) ;
0 commit comments