Skip to content

Commit c91d8ed

Browse files
committed
added .eslintrc to extend humanmade config
1 parent c663f7a commit c91d8ed

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

.eslintrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "humanmade",
3+
"globals": {
4+
"Altis": "readonly"
5+
},
6+
"rules": {
7+
"no-multi-str": "off",
8+
"no-console": "off"
9+
}
10+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"build": "webpack --config webpack.config.js",
1010
"start": "NODE_ENV=development webpack --watch --config webpack.config.js",
11-
"lint": "eslint -c humanmade src"
11+
"lint": "eslint src"
1212
},
1313
"author": "Robert O'Rourke <rob@humanmade.com>",
1414
"license": "GPL-3.0",

src/analytics.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* */
21
// Utils.
32
import './utils/polyfills';
43
import { uuid, getLanguage } from './utils';
@@ -19,7 +18,8 @@ const {
1918
Data,
2019
} = Altis.Analytics;
2120

22-
if ( !Config.PinpointId || !Config.CognitoId ) {
21+
if ( ! Config.PinpointId || ! Config.CognitoId ) {
22+
/* eslint-disable quotes */
2323
console.warn(
2424
"Altis Analytics: Missing configuration. \
2525
You must define the following constants in PHP:\n \
@@ -29,6 +29,7 @@ if ( !Config.PinpointId || !Config.CognitoId ) {
2929
define( 'ALTIS_ANALYTICS_COGNITO_REGION', '...' ); \
3030
"
3131
);
32+
/* eslint-enable quotes */
3233
}
3334

3435
/**
@@ -66,7 +67,7 @@ const params = new URLSearchParams( window.location.search );
6667
const utm = {
6768
utm_source: params.get( 'utm_source' ) || '',
6869
utm_medium: params.get( 'utm_medium' ) || '',
69-
utm_campaign: params.get( 'utm_campaign' ) || ''
70+
utm_campaign: params.get( 'utm_campaign' ) || '',
7071
};
7172

7273
/**
@@ -91,7 +92,10 @@ const getSessionID = () => {
9192
};
9293
const getSearchParams = () =>
9394
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+
} ),
9599
{}
96100
);
97101
const getAttributes = ( extra = {} ) =>
@@ -131,7 +135,7 @@ const overwriteMerge = ( destinationArray, sourceArray ) => sourceArray;
131135
const Analytics = {
132136
keys: {
133137
UserId: `aws.cognito.identity-id.${ Config.CognitoId }`,
134-
UserCredentials: `aws.cognito.identity-credentials.${ Config.CognitoId }`
138+
UserCredentials: `aws.cognito.identity-credentials.${ Config.CognitoId }`,
135139
},
136140
getUserId: () => localStorage.getItem( Analytics.keys.UserId ),
137141
getUserCredentials: () => {
@@ -140,8 +144,10 @@ const Analytics = {
140144
if ( new Date( ParsedCredentials.Credentials.Expiration ).getTime() > Date.now() ) {
141145
return ParsedCredentials;
142146
}
143-
} catch ( error ) { }
144-
return false;
147+
return false;
148+
} catch ( error ) {
149+
return false;
150+
}
145151
},
146152
setUserId: id => localStorage.setItem( Analytics.keys.UserId, id ),
147153
setUserCredentials: credentials => localStorage.setItem( Analytics.keys.UserCredentials, JSON.stringify( credentials ) ),
@@ -156,15 +162,15 @@ const Analytics = {
156162
// Configure Cognito client.
157163
const params = {
158164
region: Config.CognitoRegion,
159-
credentials: {}
165+
credentials: {},
160166
};
161167
if ( Config.CognitoEndpoint ) {
162168
params.endpoint = Config.CognitoEndpoint;
163169
}
164170
const client = new CognitoIdentityClient( params );
165171

166172
// Get unique ID if not set.
167-
if ( !UserId ) {
173+
if ( ! UserId ) {
168174
try {
169175
const getIdCommand = new GetIdCommand( { IdentityPoolId: Config.CognitoId } );
170176
const result = await client.send( getIdCommand );
@@ -196,8 +202,8 @@ const Analytics = {
196202
Analytics.client = ( async () => {
197203
// Get user credentials for pinpoint client.
198204
const Credentials = await Analytics.authenticate();
199-
if ( !Credentials ) {
200-
console.error( 'Credentials not found.', error );
205+
if ( ! Credentials ) {
206+
console.error( 'Credentials not found.' );
201207
return;
202208
}
203209

@@ -208,8 +214,8 @@ const Analytics = {
208214
accessKeyId: Credentials.AccessKeyId,
209215
secretAccessKey: Credentials.SecretKey,
210216
expiration: Credentials.Expiration,
211-
sessionToken: Credentials.SessionToken
212-
}
217+
sessionToken: Credentials.SessionToken,
218+
},
213219
};
214220
if ( Config.PinpointEndpoint ) {
215221
params.endpoint = Config.PinpointEndpoint;
@@ -226,13 +232,13 @@ const Analytics = {
226232
},
227233
getEndpoint: () => {
228234
try {
229-
const ParsedEndpoint = JSON.parse( localStorage.getItem( `aws.pinpoint.endpoint` ) );
235+
const ParsedEndpoint = JSON.parse( localStorage.getItem( 'aws.pinpoint.endpoint' ) );
230236
return ParsedEndpoint || {};
231237
} catch ( error ) {
232238
return {};
233-
};
239+
}
234240
},
235-
setEndpoint: ( endpoint ) => localStorage.setItem( `aws.pinpoint.endpoint`, JSON.stringify( endpoint ) ),
241+
setEndpoint: endpoint => localStorage.setItem( 'aws.pinpoint.endpoint', JSON.stringify( endpoint ) ),
236242
mergeEndpointData: ( endpoint = {} ) => {
237243
const Existing = Analytics.getEndpoint();
238244
const UAData = UAParser( navigator.userAgent );
@@ -276,7 +282,7 @@ const Analytics = {
276282

277283
// Merge new endpoint data with defaults.
278284
endpoint = merge.all( [ EndpointData, Existing, endpoint ], {
279-
arrayMerge: overwriteMerge
285+
arrayMerge: overwriteMerge,
280286
} );
281287

282288
// Store the endpoint data.
@@ -309,9 +315,9 @@ const Analytics = {
309315
Metrics: Object.assign( {}, data.metrics || {} ),
310316
Session: {
311317
Id: subSessionId /* required */,
312-
StartTimestamp: new Date( subSessionStart ).toISOString() /* required */
313-
}
314-
}
318+
StartTimestamp: new Date( subSessionStart ).toISOString(), /* required */
319+
},
320+
},
315321
};
316322

317323
// Add session stop parameters.
@@ -324,7 +330,7 @@ const Analytics = {
324330
Analytics.events.push( Event );
325331

326332
// Flush the events if we don't want to queue.
327-
if ( !queue ) {
333+
if ( ! queue ) {
328334
Analytics.flushEvents();
329335
return;
330336
}
@@ -342,7 +348,7 @@ const Analytics = {
342348

343349
// Events are associated with an endpoint.
344350
const UserId = Analytics.getUserId();
345-
if ( !UserId ) {
351+
if ( ! UserId ) {
346352
console.error( 'No User ID found. Make sure to call Analytics.authenticate() first.' );
347353
return;
348354
}
@@ -357,23 +363,26 @@ const Analytics = {
357363
Endpoint.RequestId = uuid();
358364

359365
// 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+
} ), {} );
361370

362371
// Build events request object.
363372
const BatchUserId = UserId.replace( `${ Config.CognitoRegion }:`, '' );
364373
const EventsRequest = {
365374
BatchItem: {
366375
[ BatchUserId ]: {
367376
Endpoint: Endpoint,
368-
Events: Events
369-
}
370-
}
377+
Events: Events,
378+
},
379+
},
371380
};
372381

373382
try {
374383
const command = new PutEventsCommand( {
375384
ApplicationId: Config.PinpointId,
376-
EventsRequest: EventsRequest
385+
EventsRequest: EventsRequest,
377386
} );
378387
const result = await client.send( command );
379388

@@ -384,7 +393,7 @@ const Analytics = {
384393
} catch ( error ) {
385394
console.error( error );
386395
}
387-
}
396+
},
388397
};
389398

390399
// Set initial endpoint data.
@@ -398,7 +407,7 @@ document.addEventListener( 'visibilitychange', () => {
398407
// Fire session stop event.
399408
Analytics.record( '_session.stop', {
400409
attributes: getAttributes( {} ),
401-
metrics: getMetrics( {} )
410+
metrics: getMetrics( {} ),
402411
} );
403412
} else {
404413
// On show reset start time.
@@ -408,7 +417,7 @@ document.addEventListener( 'visibilitychange', () => {
408417
subSessionStart = Date.now();
409418
// Fire session start event.
410419
Analytics.record( '_session.start', {
411-
attributes: getAttributes( {} )
420+
attributes: getAttributes( {} ),
412421
} );
413422
}
414423
} );
@@ -417,13 +426,13 @@ document.addEventListener( 'visibilitychange', () => {
417426
window.addEventListener( 'DOMContentLoaded', () => {
418427
// Session start.
419428
Analytics.record( '_session.start', {
420-
attributes: getAttributes()
429+
attributes: getAttributes(),
421430
} );
422431
// Record page view event & create/update endpoint immediately.
423432
Analytics.record(
424433
'pageView',
425434
{
426-
attributes: getAttributes()
435+
attributes: getAttributes(),
427436
},
428437
{},
429438
false
@@ -434,7 +443,7 @@ window.addEventListener( 'DOMContentLoaded', () => {
434443
window.addEventListener( 'beforeunload', async () => {
435444
Analytics.record( '_session.stop', {
436445
attributes: getAttributes( {} ),
437-
metrics: getMetrics( {} )
446+
metrics: getMetrics( {} ),
438447
} );
439448
await Analytics.flushEvents();
440449
} );
@@ -446,7 +455,7 @@ window.Altis.Analytics.record = ( type, data = {}, endpoint = {} ) =>
446455
type,
447456
{
448457
attributes: getAttributes( data.attributes || {} ),
449-
metrics: getMetrics( data.metrics || {} )
458+
metrics: getMetrics( data.metrics || {} ),
450459
},
451460
endpoint
452461
);

src/utils/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ export const uuid = placeholder =>
1919
export const throttle = ( delay, fn ) => {
2020
let lastCall = 0;
2121
return function ( ...args ) {
22-
const now = ( new Date ).getTime();
22+
const now = ( new Date() ).getTime();
2323
if ( now - lastCall < delay ) {
2424
return;
2525
}
2626
lastCall = now;
2727
return fn( ...args );
28-
}
29-
}
28+
};
29+
};
3030

3131
/**
3232
* Get browser locale / language.
3333
*/
34-
export const getLanguage = () => ( navigator.language || navigator.browserLanguage || ( navigator.languages || [ "en-US" ] )[ 0 ] ).toLowerCase();
34+
export const getLanguage = () => ( navigator.language || navigator.browserLanguage || ( navigator.languages || [ 'en-US' ] )[ 0 ] ).toLowerCase();

src/utils/polyfills.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77
}
88

99
function CustomEvent( event, params ) {
10-
params = params || { bubbles: false, cancelable: false, detail: null };
11-
var evt = document.createEvent( 'CustomEvent' );
10+
params = params || {
11+
bubbles: false,
12+
cancelable: false,
13+
detail: null,
14+
};
15+
let evt = document.createEvent( 'CustomEvent' );
1216
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
1317
return evt;
1418
}
1519

1620
window.CustomEvent = CustomEvent;
1721
} )();
22+
23+
window.console = window.console || {
24+
log: function () { },
25+
warn: function () { },
26+
error: function () { },
27+
};

0 commit comments

Comments
 (0)