Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sendHostname can works after 7 days and change GA Tracking ID #20

Merged
merged 8 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,47 @@
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} applicationId - application id to send
* @param {string} appName - application name
* @param {string} trackingId - GA tracking ID
* @ignore
*/
function sendHostname(applicationId) {
function sendHostname(appName, trackingId) {
var url = 'https://www.google-analytics.com/collect';
var hostname = location.hostname;
var hitType = 'event';
var trackingId = 'UA-115377265-9';
var eventCategory = 'use';
var applicationKeyForStorage = 'TOAST UI ' + appName + ' for ' + hostname + ': Statistics';
var date = window.localStorage.getItem(applicationKeyForStorage);

// skip only if the flag is defined and is set to false explicitly
// skip if the flag is defined and is set to false explicitly
if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) {
Copy link

@dongsik-yoo dongsik-yoo Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the first condition be true? This namespace tui always exist after loading tui-code-snippet.js, doesn't it?

return;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if using the global tui.usageStatics is reliable when installed by npm.
Is the tui namespace always exist when code-snippet is used?

Copy link
Contributor Author

@sohee-lee7 sohee-lee7 Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure that tui always exist. I'll revert this code.


// 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, {
Expand All @@ -34,7 +57,9 @@ function sendHostname(applicationId) {
tid: trackingId,
cid: hostname,
dp: hostname,
dh: applicationId
dh: appName,
el: appName,
ec: eventCategory
});
}
}, 1000);
Expand Down
43 changes: 43 additions & 0 deletions test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('module:request', function() {
// can not spy on imagePing. spy on appendChild instead.
spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild');
spyOn(localStorage, 'getItem').and.returnValue(null);
});

it('should call appendChild', function(done) {
Expand All @@ -51,4 +52,46 @@ describe('module:request', function() {
}, 1500);
});
});

describe('sendHostname with localstorage', function() {
beforeEach(function() {
window.tui = window.tui || {};

// can not spy on imagePing. spy on appendChild instead.
spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild');
});

it('should not call appendChild within 7 days', function(done) {
var now = new Date().getTime();
var ms6days = 6 * 24 * 60 * 60 * 1000;

spyOn(localStorage, 'getItem').and.returnValue(now - ms6days);

window.tui.usageStatistics = true;

request.sendHostname('editor');

setTimeout(function() {
expect(document.body.appendChild).not.toHaveBeenCalled();
done();
}, 1500);
});

it('should call appendChild after 7 days', function(done) {
var now = new Date().getTime();
var ms8days = 8 * 24 * 60 * 60 * 1000;

spyOn(localStorage, 'getItem').and.returnValue(now - ms8days);

window.tui.usageStatistics = true;

request.sendHostname('editor');

setTimeout(function() {
expect(document.body.appendChild).toHaveBeenCalled();
done();
}, 1500);
});
});
});