From f3183ccef93510eec41aae767a660c2a9e30d443 Mon Sep 17 00:00:00 2001 From: Dongsik Yoo Date: Mon, 3 Sep 2018 16:07:33 +0900 Subject: [PATCH 1/8] chore: reduce call of sendHostname --- src/js/request.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/js/request.js b/src/js/request.js index 6f8350e..aeb7edf 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -20,12 +20,19 @@ function sendHostname(applicationId) { var hostname = location.hostname; var hitType = 'event'; var trackingId = 'UA-115377265-9'; + var applicationKeyForStorage = 'TOAST UI ' + applicationId + ' for ' + hostname + ': Statistics'; + var alreadySentForThisApplication = window.localStorage.getItem(applicationKeyForStorage); // skip only if the flag is defined and is set to false explicitly - if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) { + if ((!type.isUndefined(window.tui) && window.tui.usageStatistics === false) + || alreadySentForThisApplication) { return; } + if (!alreadySentForThisApplication) { + window.localStorage.setItem(applicationKeyForStorage, true); + } + setTimeout(function() { if (document.readyState === 'interactive' || document.readyState === 'complete') { imagePing(url, { From f3d11f3a019fdb9670a5b140c9104617c51e4f43 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Mon, 3 Dec 2018 18:09:31 +0900 Subject: [PATCH 2/8] feat: sendHostname can works after 7 days --- src/js/request.js | 27 +++++++++++++++++++++------ test/request.test.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/js/request.js b/src/js/request.js index aeb7edf..67151bc 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -9,6 +9,19 @@ var object = require('./object'); var collection = require('./collection'); var type = require('./type'); +/** + * Check if the date has passed 7 days + * @param {number} date - milliseconds + * @returns {boolean} + * @ignore + */ +function isExpired(date) { + var now = new Date().getTime(); + var ms7days = 7 * 24 * 60 * 60 * 1000; + + return now - date > ms7days; +} + /** * Send hostname on DOMContentLoaded. * To prevent hostname set tui.usageStatistics to false. @@ -21,18 +34,20 @@ function sendHostname(applicationId) { var hitType = 'event'; var trackingId = 'UA-115377265-9'; var applicationKeyForStorage = 'TOAST UI ' + applicationId + ' for ' + hostname + ': Statistics'; - var alreadySentForThisApplication = window.localStorage.getItem(applicationKeyForStorage); + var date = window.localStorage.getItem(applicationKeyForStorage); - // skip only if the flag is defined and is set to false explicitly - if ((!type.isUndefined(window.tui) && window.tui.usageStatistics === false) - || alreadySentForThisApplication) { + // skip if the flag is defined and is set to false explicitly + if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) { return; } - if (!alreadySentForThisApplication) { - window.localStorage.setItem(applicationKeyForStorage, true); + // 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, { diff --git a/test/request.test.js b/test/request.test.js index 412e0ae..783f08b 100644 --- a/test/request.test.js +++ b/test/request.test.js @@ -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) { @@ -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); + }); + }); }); From e694675f0a991bc06394d571caa71cff3b5f876a Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Tue, 4 Dec 2018 10:36:23 +0900 Subject: [PATCH 3/8] feat: change GA Tracking ID --- src/js/request.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/js/request.js b/src/js/request.js index 67151bc..61c878d 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -9,6 +9,15 @@ var object = require('./object'); var collection = require('./collection'); var type = require('./type'); +var trackingIdMap = { + 'editor': 'UA-129966929-1', + 'grid': 'UA-129951906-1', + 'calendar': 'UA-129951699-1', + 'chart': 'UA-129983528-1', + 'image-editor': 'UA-129999381-1', + 'component': 'UA-129987462-1' +}; + /** * Check if the date has passed 7 days * @param {number} date - milliseconds @@ -32,7 +41,8 @@ function sendHostname(applicationId) { var url = 'https://www.google-analytics.com/collect'; var hostname = location.hostname; var hitType = 'event'; - var trackingId = 'UA-115377265-9'; + var eventCategory = 'use'; + var trackingId = trackingIdMap[applicationId] || trackingIdMap.component; var applicationKeyForStorage = 'TOAST UI ' + applicationId + ' for ' + hostname + ': Statistics'; var date = window.localStorage.getItem(applicationKeyForStorage); @@ -56,7 +66,9 @@ function sendHostname(applicationId) { tid: trackingId, cid: hostname, dp: hostname, - dh: applicationId + dh: applicationId, + el: applicationId, + ec: eventCategory }); } }, 1000); From af5a7eeaeb412e54dd8ca69d8f7801641720b9c4 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Wed, 5 Dec 2018 12:20:27 +0900 Subject: [PATCH 4/8] fix: apply code review --- src/js/request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/request.js b/src/js/request.js index 61c878d..146d6d5 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -17,6 +17,7 @@ var trackingIdMap = { 'image-editor': 'UA-129999381-1', 'component': 'UA-129987462-1' }; +var ms7days = 7 * 24 * 60 * 60 * 1000; /** * Check if the date has passed 7 days @@ -26,7 +27,6 @@ var trackingIdMap = { */ function isExpired(date) { var now = new Date().getTime(); - var ms7days = 7 * 24 * 60 * 60 * 1000; return now - date > ms7days; } @@ -47,7 +47,7 @@ function sendHostname(applicationId) { 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) { + if (tui.usageStatistics === false) { return; } From 2d0542f22991f85f2053142bc11c91a2729b4348 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Wed, 5 Dec 2018 12:24:20 +0900 Subject: [PATCH 5/8] fix: remove unused module --- src/js/request.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/js/request.js b/src/js/request.js index 146d6d5..b66aaa6 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -7,7 +7,6 @@ var object = require('./object'); var collection = require('./collection'); -var type = require('./type'); var trackingIdMap = { 'editor': 'UA-129966929-1', From d9002a323a5f8fc15c3eaf0e471e2eddd9234ae9 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Wed, 5 Dec 2018 15:48:43 +0900 Subject: [PATCH 6/8] Revert "fix: remove unused module" This reverts commit 2d0542f22991f85f2053142bc11c91a2729b4348. --- src/js/request.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/js/request.js b/src/js/request.js index b66aaa6..146d6d5 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -7,6 +7,7 @@ var object = require('./object'); var collection = require('./collection'); +var type = require('./type'); var trackingIdMap = { 'editor': 'UA-129966929-1', From d5df0e8691cc14c351f5be70f74784293aed9aa9 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Wed, 5 Dec 2018 15:49:00 +0900 Subject: [PATCH 7/8] Revert "fix: apply code review" This reverts commit af5a7eeaeb412e54dd8ca69d8f7801641720b9c4. --- src/js/request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/request.js b/src/js/request.js index 146d6d5..61c878d 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -17,7 +17,6 @@ var trackingIdMap = { 'image-editor': 'UA-129999381-1', 'component': 'UA-129987462-1' }; -var ms7days = 7 * 24 * 60 * 60 * 1000; /** * Check if the date has passed 7 days @@ -27,6 +26,7 @@ var ms7days = 7 * 24 * 60 * 60 * 1000; */ function isExpired(date) { var now = new Date().getTime(); + var ms7days = 7 * 24 * 60 * 60 * 1000; return now - date > ms7days; } @@ -47,7 +47,7 @@ function sendHostname(applicationId) { var date = window.localStorage.getItem(applicationKeyForStorage); // skip if the flag is defined and is set to false explicitly - if (tui.usageStatistics === false) { + if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) { return; } From 1313b4a68dd8f70b43d9c9c8394dd59f363ae212 Mon Sep 17 00:00:00 2001 From: Sohee Lee Date: Wed, 5 Dec 2018 16:11:00 +0900 Subject: [PATCH 8/8] fix: apply code review --- src/js/request.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/js/request.js b/src/js/request.js index 61c878d..5125ccc 100644 --- a/src/js/request.js +++ b/src/js/request.js @@ -8,15 +8,7 @@ var object = require('./object'); var collection = require('./collection'); var type = require('./type'); - -var trackingIdMap = { - 'editor': 'UA-129966929-1', - 'grid': 'UA-129951906-1', - 'calendar': 'UA-129951699-1', - 'chart': 'UA-129983528-1', - 'image-editor': 'UA-129999381-1', - 'component': 'UA-129987462-1' -}; +var ms7days = 7 * 24 * 60 * 60 * 1000; /** * Check if the date has passed 7 days @@ -26,7 +18,6 @@ var trackingIdMap = { */ function isExpired(date) { var now = new Date().getTime(); - var ms7days = 7 * 24 * 60 * 60 * 1000; return now - date > ms7days; } @@ -34,16 +25,16 @@ function isExpired(date) { /** * 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 eventCategory = 'use'; - var trackingId = trackingIdMap[applicationId] || trackingIdMap.component; - var applicationKeyForStorage = 'TOAST UI ' + applicationId + ' for ' + hostname + ': Statistics'; + 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 @@ -66,8 +57,8 @@ function sendHostname(applicationId) { tid: trackingId, cid: hostname, dp: hostname, - dh: applicationId, - el: applicationId, + dh: appName, + el: appName, ec: eventCategory }); }