Skip to content

Commit 6aed30e

Browse files
committed
Some initial work on metrics (WIP).
1 parent 626db5f commit 6aed30e

File tree

7 files changed

+204
-12
lines changed

7 files changed

+204
-12
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jwt.io",
3-
"version": "3.1.2",
3+
"version": "3.2.0",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/jsonwebtoken/jsonwebtoken.github.io"
@@ -14,6 +14,7 @@
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",
1718
"babel-loader": "^8.0.0-beta.0",
1819
"babel-plugin-transform-async-to-generator": "^6.24.1",
1920
"bootstrap": "^3.3.7",

src/website/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import '../google-analytics.js';
2+
import * as metrics from './metrics.js';
23
import { setupNavbar } from './navbar.js';
34
import { setupExtensionButton } from './extension.js';
45
import { setupLibraries } from './libraries.js';
56
import { setupTokenEditor, setTokenEditorValue } from '../editor';
67
import { setupJwtCounter } from './counter.js';
78
import { setupSmoothScrolling } from './smooth-scrolling.js';
89
import { setupHighlighting } from './highlighting.js';
9-
import { isChrome, isFirefox } from './utils.js';
10+
import { isChrome, isFirefox, isPartiallyInViewport, once } from './utils.js';
1011
import { setupShareJwtButton } from '../share-button.js';
1112
import {
1213
publicKeyTextArea,
1314
debuggerSection,
1415
extensionSection,
1516
ebookSection,
1617
shareJwtButton,
17-
shareJwtTextElement
18+
shareJwtTextElement,
19+
librariesElement
1820
} from './dom-elements.js';
1921

2022
import queryString from 'querystring';
@@ -36,9 +38,12 @@ function parseLocationQuery() {
3638
const token = locSearch[key] || locHash[key];
3739

3840
if(token) {
41+
metrics.track('token-in-url', { type: key });
42+
3943
setTokenEditorValue(token);
4044

4145
if(locSearch.publicKey || locHash.publicKey) {
46+
metrics.track('pubkey-in-url');
4247
publicKeyTextArea.value = locSearch.publicKey || locHash.publicKey;
4348
}
4449

@@ -51,13 +56,31 @@ function parseLocationQuery() {
5156

5257
function pickEbookOrExtensionBanner() {
5358
if((isChrome() || isFirefox()) && (Math.random() >= 0.5)) {
59+
metrics.track('extension-banner-shown');
5460
extensionSection.style.display = 'block';
5561
} else {
62+
metrics.track('ebook-banner-shown');
5663
ebookSection.style.display = 'block';
5764
}
5865
}
5966

67+
function setupMetrics() {
68+
metrics.init(PRODUCTION ? 'PROD-KEY' : 'DEV-KEY');
69+
70+
// Section visible metrics
71+
window.addEventListener('scroll', e => {
72+
if(isPartiallyInViewport(librariesElement)) {
73+
once('libraries-visible', () => metrics.track('libraries-visible-once'));
74+
}
75+
76+
if(isPartiallyInViewport(debuggerSection)) {
77+
once('debugger-visible', () => metrics.track('debugger-visible-once'));
78+
}
79+
});
80+
}
81+
6082
// Initialization
83+
setupMetrics();
6184
setupNavbar();
6285
setupExtensionButton();
6386
setupSmoothScrolling();

src/website/libraries.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { safeLocalStorageSetItem } from '../utils.js';
22
import { httpGet } from '../utils.js';
3-
import {
3+
import * as metrics from './metrics.js';
4+
import {
45
starsElements,
56
librariesElement,
6-
librariesSelect
7+
librariesSelect
78
} from './dom-elements.js';
89

910
import Isotope from 'isotope-layout';
@@ -49,7 +50,7 @@ function getStarsForGitHubRepos() {
4950

5051
const now = Date.now();
5152
try {
52-
const stored = JSON.parse(localStorage.getItem(key));
53+
const stored = JSON.parse(localStorage.getItem(key));
5354
const diff = now - stored.date;
5455
// Cached for a week
5556
const maxdiff = 7 * 24 * 60 * 60 * 1000;
@@ -71,16 +72,26 @@ function getStarsForGitHubRepos() {
7172

7273
insertStarCount(element, count);
7374
}).catch(e => {
74-
log.warn('Failed to get GitHub stars count for repository, ' +
75-
'is the repository URL OK? ', e);
75+
log.warn('Failed to get GitHub stars count for repository, ' +
76+
'is the repository URL OK? ', e);
7677
});
7778
});
7879
}
7980

81+
function setupMetrics() {
82+
// TODO for clicks
83+
}
84+
8085
export function setupLibraries() {
8186
getStarsForGitHubRepos();
82-
87+
88+
setupMetrics();
89+
8390
librariesSelect.addEventListener('change', event => {
91+
metrics.track('libraries-filter-selected', {
92+
selected: event.target.value
93+
});
94+
8495
librariesGrid.arrange({
8596
filter: event.target.value
8697
});

src/website/metrics.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Analytics from 'analytics-node';
2+
3+
let analytics;
4+
5+
export function init(key) {
6+
analytics = new Analytics(key);
7+
}
8+
9+
export function track(event, data) {
10+
if(analytics) {
11+
analytics.track(event, data);
12+
}
13+
}

src/website/utils.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ export function isInViewport(elem) {
1818
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
1919
bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
2020
);
21-
};
21+
}
22+
23+
// From: https://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling
24+
export function isPartiallyInViewport(el) {
25+
var rect = el.getBoundingClientRect();
26+
var elemTop = rect.top;
27+
var elemBottom = rect.bottom;
28+
29+
// Only completely visible elements return true:
30+
//var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
31+
// Partially visible elements return true:
32+
var isVisible = elemTop < window.innerHeight && elemBottom >= 0;
33+
return isVisible;
34+
}
2235

2336
//From:
2437
// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
@@ -29,3 +42,14 @@ export function isChrome() {
2942
export function isFirefox() {
3043
return typeof InstallTrigger !== 'undefined';
3144
}
45+
46+
const alreadyRun = {};
47+
export function once(unique, fn) {
48+
if(unique in alreadyRun) {
49+
return alreadyRun[unique];
50+
}
51+
52+
const result = fn();
53+
alreadyRun[unique] = result;
54+
return result;
55+
}

webpack.website-dev.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ module.exports = merge(common, {
1111
output: {
1212
filename: '[name].js',
1313
path: __dirname + '/dist/website/js'
14-
}
14+
},
15+
plugins: [
16+
new webpack.DefinePlugin({
17+
PRODUCTION: false
18+
})
19+
]
1520
});

0 commit comments

Comments
 (0)