1
+ /* */
1
2
// Utils.
2
- import " ./utils/polyfills" ;
3
- import { uuid , getLanguage } from " ./utils" ;
4
- import UAParser from " ua-parser-js" ;
3
+ import ' ./utils/polyfills' ;
4
+ import { uuid , getLanguage } from ' ./utils' ;
5
+ import UAParser from ' ua-parser-js' ;
5
6
import merge from 'deepmerge' ;
6
7
7
8
// AWS SDK.
8
- import { CognitoIdentityClient } from " @aws-sdk/client-cognito-identity-browser/CognitoIdentityClient" ;
9
- import { GetCredentialsForIdentityCommand } from " @aws-sdk/client-cognito-identity-browser/commands/GetCredentialsForIdentityCommand" ;
10
- import { GetIdCommand } from " @aws-sdk/client-cognito-identity-browser/commands/GetIdCommand" ;
11
- import { PinpointClient } from " @aws-sdk/client-pinpoint-browser/PinpointClient" ;
12
- import { PutEventsCommand } from " @aws-sdk/client-pinpoint-browser/commands/PutEventsCommand" ;
9
+ import { CognitoIdentityClient } from ' @aws-sdk/client-cognito-identity-browser/CognitoIdentityClient' ;
10
+ import { GetCredentialsForIdentityCommand } from ' @aws-sdk/client-cognito-identity-browser/commands/GetCredentialsForIdentityCommand' ;
11
+ import { GetIdCommand } from ' @aws-sdk/client-cognito-identity-browser/commands/GetIdCommand' ;
12
+ import { PinpointClient } from ' @aws-sdk/client-pinpoint-browser/PinpointClient' ;
13
+ import { PutEventsCommand } from ' @aws-sdk/client-pinpoint-browser/commands/PutEventsCommand' ;
13
14
14
15
const {
15
16
_attributes,
@@ -52,7 +53,7 @@ let elapsed = 0;
52
53
*/
53
54
let scrollDepthMax = 0 ;
54
55
let scrollDepthNow = 0 ;
55
- window . addEventListener ( " scroll" , ( ) => {
56
+ window . addEventListener ( ' scroll' , ( ) => {
56
57
const percent = ( window . scrollY / document . body . clientHeight ) * 100 ;
57
58
scrollDepthMax = percent > scrollDepthMax ? percent : scrollDepthMax ;
58
59
scrollDepthNow = percent ;
@@ -63,29 +64,29 @@ window.addEventListener( "scroll", () => {
63
64
*/
64
65
const params = new URLSearchParams ( window . location . search ) ;
65
66
const utm = {
66
- utm_source : params . get ( " utm_source" ) || "" ,
67
- utm_medium : params . get ( " utm_medium" ) || "" ,
68
- utm_campaign : params . get ( " utm_campaign" ) || ""
67
+ utm_source : params . get ( ' utm_source' ) || '' ,
68
+ utm_medium : params . get ( ' utm_medium' ) || '' ,
69
+ utm_campaign : params . get ( ' utm_campaign' ) || ''
69
70
} ;
70
71
71
72
/**
72
73
* Attributes helper.
73
74
*/
74
75
const getSessionID = ( ) => {
75
- if ( typeof window . sessionStorage === " undefined" ) {
76
+ if ( typeof window . sessionStorage === ' undefined' ) {
76
77
return null ;
77
78
}
78
79
79
80
// Get stored session.
80
- const sessionID = window . sessionStorage . getItem ( " _hm_uuid" ) ;
81
+ const sessionID = window . sessionStorage . getItem ( ' _hm_uuid' ) ;
81
82
82
83
if ( sessionID ) {
83
84
return sessionID ;
84
85
}
85
86
86
87
// Create and set a UUID.
87
88
const newSessionID = uuid ( ) ;
88
- window . sessionStorage . setItem ( " _hm_uuid" , newSessionID ) ;
89
+ window . sessionStorage . setItem ( ' _hm_uuid' , newSessionID ) ;
89
90
return newSessionID ;
90
91
} ;
91
92
const getSearchParams = ( ) =>
@@ -196,7 +197,7 @@ const Analytics = {
196
197
// Get user credentials for pinpoint client.
197
198
const Credentials = await Analytics . authenticate ( ) ;
198
199
if ( ! Credentials ) {
199
- console . error ( " Credentials not found." , error ) ;
200
+ console . error ( ' Credentials not found.' , error ) ;
200
201
return ;
201
202
}
202
203
@@ -238,7 +239,7 @@ const Analytics = {
238
239
const EndpointData = {
239
240
Attributes : { } ,
240
241
Demographic : {
241
- AppVersion : Data . AppVersion || "" ,
242
+ AppVersion : Data . AppVersion || '' ,
242
243
Locale : getLanguage ( ) ,
243
244
} ,
244
245
Location : { } ,
@@ -301,9 +302,9 @@ const Analytics = {
301
302
[ EventId ] : {
302
303
EventType : type /* required */ ,
303
304
Timestamp : new Date ( ) . toISOString ( ) ,
304
- AppPackageName : Data . AppPackageName || "" ,
305
- AppTitle : Data . SiteName || "" ,
306
- AppVersionCode : Data . AppVersion || "" ,
305
+ AppPackageName : Data . AppPackageName || '' ,
306
+ AppTitle : Data . SiteName || '' ,
307
+ AppVersionCode : Data . AppVersion || '' ,
307
308
Attributes : Object . assign ( { } , data . attributes || { } ) ,
308
309
Metrics : Object . assign ( { } , data . metrics || { } ) ,
309
310
Session : {
@@ -314,7 +315,7 @@ const Analytics = {
314
315
} ;
315
316
316
317
// Add session stop parameters.
317
- if ( type === " _session.stop" ) {
318
+ if ( type === ' _session.stop' ) {
318
319
Event [ EventId ] . Session . Duration = Date . now ( ) - subSessionStart ;
319
320
Event [ EventId ] . Session . StopTimestamp = new Date ( ) . toISOString ( ) ;
320
321
}
@@ -342,7 +343,7 @@ const Analytics = {
342
343
// Events are associated with an endpoint.
343
344
const UserId = Analytics . getUserId ( ) ;
344
345
if ( ! UserId ) {
345
- console . error ( " No User ID found. Make sure to call Analytics.authenticate() first." ) ;
346
+ console . error ( ' No User ID found. Make sure to call Analytics.authenticate() first.' ) ;
346
347
return ;
347
348
}
348
349
@@ -359,7 +360,7 @@ const Analytics = {
359
360
const Events = Analytics . events . reduce ( ( carry , event ) => ( { ...event , ...carry } ) , { } ) ;
360
361
361
362
// Build events request object.
362
- const BatchUserId = UserId . replace ( `${ Config . CognitoRegion } :` , "" ) ;
363
+ const BatchUserId = UserId . replace ( `${ Config . CognitoRegion } :` , '' ) ;
363
364
const EventsRequest = {
364
365
BatchItem : {
365
366
[ BatchUserId ] : {
@@ -390,12 +391,12 @@ const Analytics = {
390
391
Analytics . mergeEndpointData ( Data . Endpoint || { } ) ;
391
392
392
393
// Track sessions.
393
- document . addEventListener ( " visibilitychange" , ( ) => {
394
+ document . addEventListener ( ' visibilitychange' , ( ) => {
394
395
if ( document . hidden ) {
395
396
// On hide increment elapsed time.
396
397
elapsed += Date . now ( ) - start ;
397
398
// Fire session stop event.
398
- Analytics . record ( " _session.stop" , {
399
+ Analytics . record ( ' _session.stop' , {
399
400
attributes : getAttributes ( { } ) ,
400
401
metrics : getMetrics ( { } )
401
402
} ) ;
@@ -406,21 +407,21 @@ document.addEventListener( "visibilitychange", () => {
406
407
subSessionId = uuid ( ) ;
407
408
subSessionStart = Date . now ( ) ;
408
409
// Fire session start event.
409
- Analytics . record ( " _session.start" , {
410
+ Analytics . record ( ' _session.start' , {
410
411
attributes : getAttributes ( { } )
411
412
} ) ;
412
413
}
413
414
} ) ;
414
415
415
416
// Start recording after document loaded and tests applied.
416
- window . addEventListener ( " DOMContentLoaded" , ( ) => {
417
+ window . addEventListener ( ' DOMContentLoaded' , ( ) => {
417
418
// Session start.
418
- Analytics . record ( " _session.start" , {
419
+ Analytics . record ( ' _session.start' , {
419
420
attributes : getAttributes ( )
420
421
} ) ;
421
422
// Record page view event & create/update endpoint immediately.
422
423
Analytics . record (
423
- " pageView" ,
424
+ ' pageView' ,
424
425
{
425
426
attributes : getAttributes ( )
426
427
} ,
@@ -430,8 +431,8 @@ window.addEventListener( "DOMContentLoaded", () => {
430
431
} ) ;
431
432
432
433
// Flush remaining events.
433
- window . addEventListener ( " beforeunload" , async ( ) => {
434
- Analytics . record ( " _session.stop" , {
434
+ window . addEventListener ( ' beforeunload' , async ( ) => {
435
+ Analytics . record ( ' _session.stop' , {
435
436
attributes : getAttributes ( { } ) ,
436
437
metrics : getMetrics ( { } )
437
438
} ) ;
0 commit comments