-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathsendHostname.js
More file actions
67 lines (56 loc) · 1.64 KB
/
sendHostname.js
File metadata and controls
67 lines (56 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @fileoverview Send hostname on DOMContentLoaded.
*/
'use strict';
var isUndefined = require('../type/isUndefined');
var imagePing = require('./imagePing');
var ms7days = 7 * 24 * 60 * 60 * 1000;
/**
* Check if the date has passed 7 days
* @param {number} date - milliseconds
* @returns {boolean}
* @private
*/
function isExpired(date) {
var now = new Date().getTime();
return now - date > ms7days;
}
/**
* Send hostname on DOMContentLoaded.
* To prevent hostname set tui.usageStatistics to false.
* @param {string} appName - application name
* @param {string} trackingId - GA tracking ID
* @ignore
*/
function sendHostname(appName, trackingId) {
var url = 'https://www.google-analytics.com/collect';
var hostname = location.hostname;
var hitType = 'event';
var eventCategory = 'use';
var applicationKeyForStorage = 'TOAST UI ' + appName + ' for ' + hostname + ': Statistics';
var date = window.localStorage.getItem(applicationKeyForStorage);
// skip if the flag is defined and is set to false explicitly
if (!isUndefined(window.tui) && window.tui.usageStatistics === false) {
return;
}
// skip if not pass seven days old
if (date && !isExpired(date)) {
return;
}
window.localStorage.setItem(applicationKeyForStorage, new Date().getTime());
setTimeout(function() {
if (document.readyState === 'interactive' || document.readyState === 'complete') {
imagePing(url, {
v: 1,
t: hitType,
tid: trackingId,
cid: hostname,
dp: hostname,
dh: appName,
el: appName,
ec: eventCategory
});
}
}, 1000);
}
module.exports = sendHostname;