Skip to content

Commit 1173704

Browse files
committed
Update: Added GA enabled / disabled state support
1 parent 26360c5 commit 1173704

File tree

2 files changed

+112
-29
lines changed

2 files changed

+112
-29
lines changed

scripts/arc-preferences.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,16 @@ class ArcPreferences extends ArcBase {
6464
updateSettings() {
6565
return this.storeFile(this.settingsFile, this.__settings);
6666
}
67-
67+
/**
68+
* Creates default settings object.
69+
*/
6870
defaultSettings() {
6971
return {
7072
'useVariables': true,
7173
'useCookieStorage': true,
7274
'requestDefaultTimeout': 45,
73-
'autoUpdate': true
75+
'autoUpdate': true,
76+
'telemetry': true
7477
};
7578
}
7679

src/arc-electron.html

Lines changed: 107 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,32 @@
162162
<paper-toast id="errorToast" duration="5000"></paper-toast>
163163
<paper-toast id="offlineToast" class="offline-toast" text="You are now offline. Can't make new requests."></paper-toast>
164164
<search-bar id="content-search-bar" on-iron-overlay-opened="textSearchBarOpened"></search-bar>
165-
<app-analytics tracking-id="UA-18021184-6" app-name="ARC-electon" app-id="[[appId]]" app-version="[[appVersion]]" disable-tracking="{{analyticsDisabled}}" data-source="electron-app">
166-
<template is="dom-repeat" items="[[gaCustomMetrics]]">
167-
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
168-
</template>
169-
<template is="dom-repeat" items="[[gaCustomDimensions]]">
170-
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
171-
</template>
172-
</app-analytics>
173-
<app-analytics tracking-id="UA-18021184-14" app-name="ARC" app-id="[[appId]]" app-version="[[appVersion]]" data-source="electron-app" disable-tracking="{{analyticsDisabled}}">
174-
<template is="dom-repeat" items="[[gaCustomMetrics]]">
175-
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
176-
</template>
177-
<template is="dom-repeat" items="[[gaCustomDimensions]]">
178-
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
179-
</template>
180-
</app-analytics>
181-
<app-analytics tracking-id="UA-71458341-2" app-name="ARC" app-id="[[appId]]" app-version="[[appVersion]]" data-source="electron-app" disable-tracking="{{analyticsDisabled}}">
182-
<template is="dom-repeat" items="[[gaCustomMetrics]]">
183-
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
184-
</template>
185-
<template is="dom-repeat" items="[[gaCustomDimensions]]">
186-
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
187-
</template>
188-
</app-analytics>
165+
<template is="dom-if" if="[[telemetry]]" restamp="true">
166+
<app-analytics tracking-id="UA-18021184-6" app-name="ARC-electon" app-id="[[appId]]" app-version="[[appVersion]]" disable-tracking="{{analyticsDisabled}}" data-source="electron-app">
167+
<template is="dom-repeat" items="[[gaCustomMetrics]]">
168+
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
169+
</template>
170+
<template is="dom-repeat" items="[[gaCustomDimensions]]">
171+
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
172+
</template>
173+
</app-analytics>
174+
<app-analytics tracking-id="UA-18021184-14" app-name="ARC" app-id="[[appId]]" app-version="[[appVersion]]" data-source="electron-app" disable-tracking="{{analyticsDisabled}}">
175+
<template is="dom-repeat" items="[[gaCustomMetrics]]">
176+
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
177+
</template>
178+
<template is="dom-repeat" items="[[gaCustomDimensions]]">
179+
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
180+
</template>
181+
</app-analytics>
182+
<app-analytics tracking-id="UA-71458341-2" app-name="ARC" app-id="[[appId]]" app-version="[[appVersion]]" data-source="electron-app" disable-tracking="{{analyticsDisabled}}">
183+
<template is="dom-repeat" items="[[gaCustomMetrics]]">
184+
<app-analytics-custom type="metric" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
185+
</template>
186+
<template is="dom-repeat" items="[[gaCustomDimensions]]">
187+
<app-analytics-custom type="dimension" index="[[item.index]]" value="[[item.value]]"></app-analytics-custom>
188+
</template>
189+
</app-analytics>
190+
</template>
189191
<app-logger></app-logger>
190192
<arc-definitions></arc-definitions>
191193
<variables-evaluator no-before-request></variables-evaluator>
@@ -245,7 +247,23 @@
245247
_drawerInitTransition: Number,
246248
accessToken: String,
247249
// Latest update event data
248-
updateState: Array
250+
updateState: Array,
251+
/**
252+
* Overrides analytics element behavior.
253+
* This setting is now in main application settings.
254+
*
255+
* TODO: Remove state setting from the element. This should be
256+
* controlled from the outside.
257+
*/
258+
analyticsDisabled: {
259+
type: Boolean,
260+
value: false
261+
},
262+
/**
263+
* Sets Google Analytics state. If this is not set or falsy, then Google
264+
* Analytics is diosabled (element is removed from the DOM).
265+
*/
266+
telemetry: Boolean
249267
},
250268

251269
created: function() {
@@ -273,7 +291,8 @@
273291
'google-autorize': '_googleOauthTokenRequested',
274292
'on-process-incoming-data': '_processIcomingData',
275293
'pick-google-drive-item': 'openDrivePicker',
276-
'open-drive-selector': 'openDrivePicker'
294+
'open-drive-selector': 'openDrivePicker',
295+
'settings-changed': '_settingChanged'
277296
},
278297

279298
observers: [
@@ -297,8 +316,41 @@
297316
this._requestAuthToken(false);
298317
this.setupAnalytics();
299318
},
300-
319+
/**
320+
* Reads current settings and enables Google Analytics if it's not
321+
* disabled by the uers.
322+
*/
301323
setupAnalytics: function() {
324+
var event = this.fire('settings-read', {}, {
325+
composed: true,
326+
cancelable: true
327+
});
328+
if (!event.defaultPrevented) {
329+
throw new Error('Settings provided not found.');
330+
}
331+
return event.detail.result.then(settings => {
332+
if (settings.telemetry === false) {
333+
this._analyticsDisabled();
334+
} else {
335+
this._analyticsEnabled();
336+
}
337+
});
338+
},
339+
/**
340+
* Enables Google Analytics.
341+
*
342+
* Until settings status is checked Google Analytics components is not
343+
* even in the DOM. Once the `telemetry` flag is set analytics elements
344+
* are attached to the DOM and start handling events.
345+
*
346+
* This can be disabled by calling `_analyticsDisabled` or by sending
347+
* `settings-changed` custom event with `telemetry` key.
348+
*/
349+
_analyticsEnabled: function() {
350+
this.telemetry = true;
351+
if (this.gaCustomDimensions) {
352+
return;
353+
}
302354
this.gaCustomDimensions = [];
303355
this.push('gaCustomDimensions', {
304356
index: 1,
@@ -313,6 +365,34 @@
313365
value: 'stable'
314366
});
315367
},
368+
// To be called if GA is disabled to remove GA elements from the DOM.
369+
_analyticsDisabled: function() {
370+
this.telemetry = false;
371+
// no need to clean up custom dimensions
372+
// since it is removed from DOM anyway.
373+
},
374+
/**
375+
* Handles settings change event.
376+
* If changed setting is `telemetry` then it upodates GA state.
377+
*/
378+
_settingChanged: function(e) {
379+
if (e.cancelable) {
380+
return; // request for change
381+
}
382+
// Here we just care about `telemetry`
383+
if (e.detail.name !== 'telemetry') {
384+
return;
385+
}
386+
var value = e.detail.value;
387+
if (typeof value !== 'boolean') {
388+
value = value === 'false' ? false : true;
389+
}
390+
if (value) {
391+
this._analyticsEnabled();
392+
} else {
393+
this._analyticsDisabled();
394+
}
395+
},
316396

317397
_setupRequestRoute: function(record) {
318398
var params = record && record.base;

0 commit comments

Comments
 (0)