Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 3d93242

Browse files
authored
Merge pull request #418 from googlecodelabs/nicolasgarnier-patch-8
Guard against analytics not existing
2 parents 116c679 + f9cd68c commit 3d93242

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

codelab-elements/google-codelab-analytics/google_codelab_analytics.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,12 @@ class CodelabAnalytics extends HTMLElement {
253253
/** @private */
254254
gaSend_(params) {
255255
window['ga'](function() {
256-
const trackers = window['ga'].getAll();
257-
trackers.forEach((tracker) => {
258-
tracker.send(params);
259-
});
256+
if (window['ga'].getAll) {
257+
const trackers = window['ga'].getAll();
258+
trackers.forEach((tracker) => {
259+
tracker.send(params);
260+
});
261+
}
260262
});
261263
}
262264

@@ -330,15 +332,17 @@ class CodelabAnalytics extends HTMLElement {
330332

331333
/** @private */
332334
createTrackers_() {
333-
// The default tracker is given name 't0' per analytics.js dev docs.
334-
if (this.gaid_ && !this.isTrackerCreated_(this.gaid_)) {
335-
window['ga']('create', this.gaid_, 'auto');
336-
}
335+
if (window['ga']) {
336+
// The default tracker is given name 't0' per analytics.js dev docs.
337+
if (this.gaid_ && !this.isTrackerCreated_(this.gaid_)) {
338+
window['ga']('create', this.gaid_, 'auto');
339+
}
337340

338-
const gaView = this.getGAView_();
339-
if (gaView && !this.isTrackerCreated_(gaView)) {
340-
window['ga']('create', gaView, 'auto', 'view');
341-
window['ga']('view.send', 'pageview');
341+
const gaView = this.getGAView_();
342+
if (gaView && !this.isTrackerCreated_(gaView)) {
343+
window['ga']('create', gaView, 'auto', 'view');
344+
window['ga']('view.send', 'pageview');
345+
}
342346
}
343347

344348
this.createCodelabGATracker_();
@@ -349,9 +353,11 @@ class CodelabAnalytics extends HTMLElement {
349353
* @private
350354
*/
351355
createCodelabGATracker_() {
352-
const codelabGAId = this.getAttribute(CODELAB_GAID_ATTR);
353-
if (codelabGAId && !this.isTrackerCreated_(codelabGAId)) {
354-
window['ga']('create', codelabGAId, 'auto', 'codelabAccount');
356+
if (window['ga']) {
357+
const codelabGAId = this.getAttribute(CODELAB_GAID_ATTR);
358+
if (codelabGAId && !this.isTrackerCreated_(codelabGAId)) {
359+
window['ga']('create', codelabGAId, 'auto', 'codelabAccount');
360+
}
355361
}
356362
}
357363

@@ -361,13 +367,15 @@ class CodelabAnalytics extends HTMLElement {
361367
* @private
362368
*/
363369
isTrackerCreated_(trackerId) {
364-
const allTrackers = window['ga'].getAll();
365370
let isCreated = false;
366-
allTrackers.forEach((tracker) => {
367-
if (tracker.get('trackingId') == trackerId) {
368-
isCreated = true;
369-
}
370-
});
371+
if (window['ga'] && window['ga'].getAll) {
372+
const allTrackers = window['ga'].getAll();
373+
allTrackers.forEach((tracker) => {
374+
if (tracker.get('trackingId') == trackerId) {
375+
isCreated = true;
376+
}
377+
});
378+
}
371379
return isCreated;
372380
}
373381
}

0 commit comments

Comments
 (0)