Skip to content

Commit 357517c

Browse files
committed
Metrics appear to be working locally.
1 parent 42e6905 commit 357517c

File tree

10 files changed

+125
-141
lines changed

10 files changed

+125
-141
lines changed

package-lock.json

Lines changed: 1 addition & 116 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"@babel/core": "^7.0.0-beta.36",
1515
"@babel/polyfill": "^7.0.0-beta.37",
1616
"@babel/preset-env": "^7.0.0-beta.36",
17-
"analytics-node": "^3.3.0",
1817
"babel-loader": "^8.0.0-beta.0",
1918
"babel-plugin-transform-async-to-generator": "^6.24.1",
2019
"bootstrap": "^3.3.7",

src/editor/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ import log from 'loglevel';
4848
// passed to the event manager.
4949
const eventManager = new EventManager();
5050

51-
function trackToken(jwt, operation) {
51+
function trackToken(jwt, operation, extra) {
5252
const tokenInfo = getSafeTokenInfo(jwt);
5353

54-
metric.track('editor-jwt-tracked', {
54+
metrics.track('editor-jwt-tracked', Object.assign({
5555
operation: operation,
5656
tokenInfo: tokenInfo
57-
});
57+
}, extra));
5858

5959
return tokenInfo.hash;
6060
}
@@ -238,7 +238,9 @@ function encodeToken() {
238238
sign(header, payload, key, secretBase64Checkbox.checked).then(encoded => {
239239
eventManager.withDisabledEvents(() => {
240240
tokenEditor.setValue(encoded);
241-
trackToken(encoded, 'encode');
241+
trackToken(encoded, 'encode', {
242+
secretBase64Checkbox: secretBase64Checkbox.checked
243+
});
242244
});
243245
}).catch(e => {
244246
eventManager.withDisabledEvents(() => {
@@ -324,13 +326,15 @@ function verifyToken() {
324326
if(valid) {
325327
markAsValid();
326328
metrics.track('editor-jwt-verified', {
327-
tokenHash: tokenHash
329+
tokenHash: tokenHash,
330+
secretBase64Checkbox: secretBase64Checkbox.checked
328331
});
329332
} else {
330333
markAsInvalid();
331334
metrics.track('editor-jwt-invalid', {
332335
reason: 'invalid signature',
333-
tokenHash: tokenHash
336+
tokenHash: tokenHash,
337+
secretBase64Checkbox: secretBase64Checkbox.checked
334338
});
335339
}
336340
});

src/editor/utils.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,21 @@ export function isString(value) {
5555
}
5656

5757
function getBase64Format(token) {
58-
if(jwt.isValidBase64String(token, true)) {
59-
return 'base64url';
60-
} else if(jwt.isValidBase64String(token, false)) {
61-
return 'base64';
62-
} else {
58+
try {
59+
function getFormat(str) {
60+
if(jwt.isValidBase64String(str, true)) {
61+
return 'base64url';
62+
} else if(jwt.isValidBase64String(str, false)) {
63+
return 'base64';
64+
} else {
65+
return 'invalid';
66+
}
67+
}
68+
69+
const formats = token.split('.').map(getFormat);
70+
return formats.every(r => r === formats[0]) ?
71+
formats[0] : 'invalid';
72+
} catch(e) {
6373
return 'invalid';
6474
}
6575
}
@@ -112,7 +122,7 @@ export function getSafeTokenInfo(token) {
112122
try {
113123
const decoded = jwt.decode(token);
114124

115-
const result = Object.assign(result, {
125+
Object.assign(result, {
116126
decodedWithErrors: decoded.errors,
117127
encodedSize: token.length,
118128
base64Format: getBase64Format(token),

src/metrics.js

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,96 @@
1-
import Analytics from 'analytics-node';
21
import log from 'loglevel';
32

4-
let analytics;
3+
export function init(apiKey) {
4+
// Create a queue, but don't obliterate an existing one!
5+
var analytics = window.analytics = window.analytics || [];
56

6-
export function init(key) {
7-
analytics = new Analytics(key);
7+
// If the real analytics.js is already on the page return.
8+
if (analytics.initialize) return;
9+
10+
// If the snippet was invoked already show an error.
11+
if (analytics.invoked) {
12+
if (window.console && console.error) {
13+
console.error('Segment snippet included twice.');
14+
}
15+
return;
16+
}
17+
18+
// Invoked flag, to make sure the snippet
19+
// is never invoked twice.
20+
analytics.invoked = true;
21+
22+
// A list of the methods in Analytics.js to stub.
23+
analytics.methods = [
24+
'trackSubmit',
25+
'trackClick',
26+
'trackLink',
27+
'trackForm',
28+
'pageview',
29+
'identify',
30+
'reset',
31+
'group',
32+
'track',
33+
'ready',
34+
'alias',
35+
'debug',
36+
'page',
37+
'once',
38+
'off',
39+
'on'
40+
];
41+
42+
// Define a factory to create stubs. These are placeholders
43+
// for methods in Analytics.js so that you never have to wait
44+
// for it to load to actually record data. The `method` is
45+
// stored as the first argument, so we can replay the data.
46+
analytics.factory = function(method){
47+
return function(){
48+
var args = Array.prototype.slice.call(arguments);
49+
args.unshift(method);
50+
analytics.push(args);
51+
return analytics;
52+
};
53+
};
54+
55+
// For each of our methods, generate a queueing stub.
56+
for (var i = 0; i < analytics.methods.length; i++) {
57+
var key = analytics.methods[i];
58+
analytics[key] = analytics.factory(key);
59+
}
60+
61+
// Define a method to load Analytics.js from our CDN,
62+
// and that will be sure to only ever load it once.
63+
analytics.load = function(key, options){
64+
// Create an async script element based on your key.
65+
var script = document.createElement('script');
66+
script.type = 'text/javascript';
67+
script.async = true;
68+
script.src = 'https://cdn.segment.com/analytics.js/v1/'
69+
+ key + '/analytics.min.js';
70+
71+
// Insert our script next to the first script element.
72+
var first = document.getElementsByTagName('script')[0];
73+
first.parentNode.insertBefore(script, first);
74+
analytics._loadOptions = options;
75+
};
76+
77+
// Add a version to keep track of what's in the wild.
78+
analytics.SNIPPET_VERSION = '4.1.0';
79+
80+
// Load Analytics.js with your key, which will automatically
81+
// load the tools you've enabled for your account. Boosh!
82+
analytics.load(apiKey);
83+
84+
// Make the first page call to load the integrations. If
85+
// you'd like to manually name or tag the page, edit or
86+
// move this call however you'd like.
87+
analytics.page();
888
}
989

1090
export function track(event, data) {
11-
if(analytics) {
91+
if(window.analytics) {
1292
try {
13-
analytics.track(event, data);
93+
window.analytics.track(event, data);
1494
} catch(e) {
1595
log.error(`Metrics library error for event ${event}: ${e}`);
1696
}

src/website/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ function pickEbookOrExtensionBanner() {
6565
}
6666

6767
function setupMetrics() {
68-
metrics.init(PRODUCTION ? 'PROD-KEY' : 'DEV-KEY');
68+
metrics.init(PRODUCTION ?
69+
'693wciz6BgZdlmW0qDlbgxpyj7gtVxRR' :
70+
'xBxBArADMejN3aqs9NhISky9PHPXIr80');
6971

7072
// Section visible metrics
7173
window.addEventListener('scroll', e => {

0 commit comments

Comments
 (0)