Skip to content
Permalink
Browse files

Chore: Used shared instrumentation key in the browser

Also rename `storage` variables for clarity.
  • Loading branch information
antross authored and molant committed Nov 11, 2019
1 parent 3f9aec8 commit 2dfc8d6afde8ae66caa54953bb2fb154c7414224
Showing with 19 additions and 22 deletions.
  1. +19 −22 packages/extension-browser/src/devtools/utils/analytics.ts
@@ -2,24 +2,21 @@ import { Config, ErrorData, Results } from '../../shared/types';

import { getUpdatedActivity, initTelemetry, updateTelemetry, trackEvent } from '@hint/utils-telemetry';
import { determineHintStatus } from './hints';
import * as storage from './storage';
import * as localstore from './storage';

const manifest = require('../../manifest.json');
const activityKey = 'webhint-activity';
const storageKey = 'webhint-telemetry';
const alreadyOptInKey = 'webhint-already-opt-in';

const instrumentationKey = '8ef2b55b-2ce9-4c33-a09a-2c3ef605c97d';

/** Check if telemetry is enabled */
export const enabled = (s = storage) => {
return !!s.getItem(storageKey);
export const enabled = (storage = localstore) => {
return !!storage.getItem(storageKey);
};

initTelemetry({
defaultProperties: { 'extension-version': manifest.version },
enabled: enabled(),
instrumentationKey,
post: async (url, data) => {
const response = await fetch(url, { body: data, method: 'POST' });

@@ -31,13 +28,13 @@ initTelemetry({
* Return true if the user has not respond yet
* to opt-in.
*/
export const showOptIn = (s = storage) => {
return s.getItem(storageKey) === undefined;
export const showOptIn = (storage = localstore) => {
return storage.getItem(storageKey) === undefined;
};

/** Called to initialize the underlying analytics library. */
export const setup = (s = storage) => {
const telemetry = s.getItem(storageKey);
export const setup = (storage = localstore) => {
const telemetry = storage.getItem(storageKey);

if (!telemetry) {
console.log('telemetry disabled');
@@ -51,40 +48,40 @@ export const setup = (s = storage) => {
};

/** Enables telemetry */
export const enable = (s = storage) => {
s.setItem(storageKey, true);
export const enable = (storage = localstore) => {
storage.setItem(storageKey, true);

setup(s);
setup(storage);

// If it is the first time the user enable telemetry
if (!s.getItem(alreadyOptInKey)) {
s.setItem(alreadyOptInKey, true);
if (!storage.getItem(alreadyOptInKey)) {
storage.setItem(alreadyOptInKey, true);
trackEvent('f12-telemetry');
}
};

/** Disables telemetry */
export const disable = (s = storage) => {
s.setItem(storageKey, false);
export const disable = (storage = localstore) => {
storage.setItem(storageKey, false);

setup(s);
setup(storage);
};

/**
* Report once per UTC day that a user is active (has run a scan).
* Data includes `last28Days` (e.g. `"1001100110011001100110011001"`)
* and `lastUpdated` (e.g. `"2019-10-04T00:00:00.000Z"`).
*/
const trackActive = (s = storage) => {
const trackActive = (storage = localstore) => {
// Don't count a user as active if telemetry is disabled.
if (!enabled(s)) {
if (!enabled(storage)) {
return;
}

const activity = getUpdatedActivity(s.getItem(activityKey));
const activity = getUpdatedActivity(storage.getItem(activityKey));

if (activity) {
s.setItem(activityKey, activity);
storage.setItem(activityKey, activity);
trackEvent('f12-activity', activity);
}
};

0 comments on commit 2dfc8d6

Please sign in to comment.
You can’t perform that action at this time.