request.js
2.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @fileoverview This module has some functions for handling object as collection.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var object = require('./object');
var collection = require('./collection');
var type = require('./type');
var ms7days = 7 * 24 * 60 * 60 * 1000;
/**
* Check if the date has passed 7 days
* @param {number} date - milliseconds
* @returns {boolean}
* @ignore
*/
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 (!type.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);
}
/**
* Request image ping.
* @param {String} url url for ping request
* @param {Object} trackingInfo infos for make query string
* @returns {HTMLElement}
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* util.imagePing('https://www.google-analytics.com/collect', {
* v: 1,
* t: 'event',
* tid: 'trackingid',
* cid: 'cid',
* dp: 'dp',
* dh: 'dh'
* });
*/
function imagePing(url, trackingInfo) {
var queryString = collection.map(object.keys(trackingInfo), function(key, index) {
var startWith = index === 0 ? '' : '&';
return startWith + key + '=' + trackingInfo[key];
}).join('');
var trackingElement = document.createElement('img');
trackingElement.src = url + '?' + queryString;
trackingElement.style.display = 'none';
document.body.appendChild(trackingElement);
document.body.removeChild(trackingElement);
return trackingElement;
}
module.exports = {
imagePing: imagePing,
sendHostname: sendHostname
};