Skip to content

Commit bdda85c

Browse files
authored
feat: sendHostname can works after 7 days and change GA Tracking ID (#20)
* chore: reduce call of sendHostname * feat: sendHostname can works after 7 days * feat: change GA Tracking ID * fix: apply code review * fix: remove unused module * Revert "fix: remove unused module" This reverts commit 2d0542f. * Revert "fix: apply code review" This reverts commit af5a7ee. * fix: apply code review
1 parent 713a9e4 commit bdda85c

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/js/request.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,47 @@
88
var object = require('./object');
99
var collection = require('./collection');
1010
var type = require('./type');
11+
var ms7days = 7 * 24 * 60 * 60 * 1000;
12+
13+
/**
14+
* Check if the date has passed 7 days
15+
* @param {number} date - milliseconds
16+
* @returns {boolean}
17+
* @ignore
18+
*/
19+
function isExpired(date) {
20+
var now = new Date().getTime();
21+
22+
return now - date > ms7days;
23+
}
1124

1225
/**
1326
* Send hostname on DOMContentLoaded.
1427
* To prevent hostname set tui.usageStatistics to false.
15-
* @param {string} applicationId - application id to send
28+
* @param {string} appName - application name
29+
* @param {string} trackingId - GA tracking ID
1630
* @ignore
1731
*/
18-
function sendHostname(applicationId) {
32+
function sendHostname(appName, trackingId) {
1933
var url = 'https://www.google-analytics.com/collect';
2034
var hostname = location.hostname;
2135
var hitType = 'event';
22-
var trackingId = 'UA-115377265-9';
36+
var eventCategory = 'use';
37+
var applicationKeyForStorage = 'TOAST UI ' + appName + ' for ' + hostname + ': Statistics';
38+
var date = window.localStorage.getItem(applicationKeyForStorage);
2339

24-
// skip only if the flag is defined and is set to false explicitly
40+
// skip if the flag is defined and is set to false explicitly
2541
if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) {
2642
return;
2743
}
2844

45+
// skip if not pass seven days old
46+
if (date && !isExpired(date)) {
47+
return;
48+
}
49+
50+
window.localStorage.setItem(applicationKeyForStorage, new Date().getTime());
51+
2952
setTimeout(function() {
3053
if (document.readyState === 'interactive' || document.readyState === 'complete') {
3154
imagePing(url, {
@@ -34,7 +57,9 @@ function sendHostname(applicationId) {
3457
tid: trackingId,
3558
cid: hostname,
3659
dp: hostname,
37-
dh: applicationId
60+
dh: appName,
61+
el: appName,
62+
ec: eventCategory
3863
});
3964
}
4065
}, 1000);

test/request.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('module:request', function() {
2929
// can not spy on imagePing. spy on appendChild instead.
3030
spyOn(document.body, 'appendChild');
3131
spyOn(document.body, 'removeChild');
32+
spyOn(localStorage, 'getItem').and.returnValue(null);
3233
});
3334

3435
it('should call appendChild', function(done) {
@@ -51,4 +52,46 @@ describe('module:request', function() {
5152
}, 1500);
5253
});
5354
});
55+
56+
describe('sendHostname with localstorage', function() {
57+
beforeEach(function() {
58+
window.tui = window.tui || {};
59+
60+
// can not spy on imagePing. spy on appendChild instead.
61+
spyOn(document.body, 'appendChild');
62+
spyOn(document.body, 'removeChild');
63+
});
64+
65+
it('should not call appendChild within 7 days', function(done) {
66+
var now = new Date().getTime();
67+
var ms6days = 6 * 24 * 60 * 60 * 1000;
68+
69+
spyOn(localStorage, 'getItem').and.returnValue(now - ms6days);
70+
71+
window.tui.usageStatistics = true;
72+
73+
request.sendHostname('editor');
74+
75+
setTimeout(function() {
76+
expect(document.body.appendChild).not.toHaveBeenCalled();
77+
done();
78+
}, 1500);
79+
});
80+
81+
it('should call appendChild after 7 days', function(done) {
82+
var now = new Date().getTime();
83+
var ms8days = 8 * 24 * 60 * 60 * 1000;
84+
85+
spyOn(localStorage, 'getItem').and.returnValue(now - ms8days);
86+
87+
window.tui.usageStatistics = true;
88+
89+
request.sendHostname('editor');
90+
91+
setTimeout(function() {
92+
expect(document.body.appendChild).toHaveBeenCalled();
93+
done();
94+
}, 1500);
95+
});
96+
});
5497
});

0 commit comments

Comments
 (0)