Skip to content

Commit f3d11f3

Browse files
author
Sohee Lee
committed
feat: sendHostname can works after 7 days
1 parent f3183cc commit f3d11f3

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/js/request.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ var object = require('./object');
99
var collection = require('./collection');
1010
var type = require('./type');
1111

12+
/**
13+
* Check if the date has passed 7 days
14+
* @param {number} date - milliseconds
15+
* @returns {boolean}
16+
* @ignore
17+
*/
18+
function isExpired(date) {
19+
var now = new Date().getTime();
20+
var ms7days = 7 * 24 * 60 * 60 * 1000;
21+
22+
return now - date > ms7days;
23+
}
24+
1225
/**
1326
* Send hostname on DOMContentLoaded.
1427
* To prevent hostname set tui.usageStatistics to false.
@@ -21,18 +34,20 @@ function sendHostname(applicationId) {
2134
var hitType = 'event';
2235
var trackingId = 'UA-115377265-9';
2336
var applicationKeyForStorage = 'TOAST UI ' + applicationId + ' for ' + hostname + ': Statistics';
24-
var alreadySentForThisApplication = window.localStorage.getItem(applicationKeyForStorage);
37+
var date = window.localStorage.getItem(applicationKeyForStorage);
2538

26-
// skip only if the flag is defined and is set to false explicitly
27-
if ((!type.isUndefined(window.tui) && window.tui.usageStatistics === false)
28-
|| alreadySentForThisApplication) {
39+
// skip if the flag is defined and is set to false explicitly
40+
if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) {
2941
return;
3042
}
3143

32-
if (!alreadySentForThisApplication) {
33-
window.localStorage.setItem(applicationKeyForStorage, true);
44+
// skip if not pass seven days old
45+
if (date && !isExpired(date)) {
46+
return;
3447
}
3548

49+
window.localStorage.setItem(applicationKeyForStorage, new Date().getTime());
50+
3651
setTimeout(function() {
3752
if (document.readyState === 'interactive' || document.readyState === 'complete') {
3853
imagePing(url, {

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)