Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
74 changes: 73 additions & 1 deletion src/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@

var object = require('./object');
var collection = require('./collection');
var type = require('./type');

/**
* Send hostname on DOMContentLoaded.
* To prevent hostname set tui.usageStatistics to false.
* @param {string} applicationId - application id to send
* @ignore
*/
function sendHostname(applicationId) {
// skip only if the flag is defined and is set to false explicitly
if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) {
return;
}

contentLoaded(window, function() {
var url = 'https://www.google-analytics.com/collect';
var hostname = location.hostname;
var hitType = 'event';
var trackingId = 'UA-115377265-9';

imagePing(url, {
v: 1,
t: hitType,
tid: trackingId,
cid: hostname,
dp: hostname,
dh: applicationId
});
});
}

/**
* Request image ping.
Expand Down Expand Up @@ -46,6 +76,48 @@ function imagePing(url, trackingInfo) {
return trackingElement;
}

/**
* Cross-browser contentLoaded handler
* adopted from https://github.com/dperini/ContentLoaded/blob/master/src/contentloaded.js
* @ignore
*/
/* eslint-disable */
function contentLoaded(win, fn) {
Copy link
Member

Choose a reason for hiding this comment

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

음.. 이거 복붙하는게 아니라 모듈로 가져온 다음에 external 처리 안해주면 되지 않나요??

var done = false, top = true,

doc = win.document,
root = doc.documentElement,
modern = doc.addEventListener,

add = modern ? 'addEventListener' : 'attachEvent',
rem = modern ? 'removeEventListener' : 'detachEvent',
pre = modern ? '' : 'on',

init = function(e) {
if (e.type == 'readystatechange' && doc.readyState != 'complete') return;
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
if (!done && (done = true)) fn.call(win, e.type || e);
},

poll = function() {
try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
init('poll');
};

if (doc.readyState == 'complete') fn.call(win, 'lazy');
else {
if (!modern && root.doScroll) {
try { top = !win.frameElement; } catch(e) { }
if (top) poll();
}
doc[add](pre + 'DOMContentLoaded', init, false);
doc[add](pre + 'readystatechange', init, false);
win[add](pre + 'load', init, false);
}
}
/* eslint-enable */

module.exports = {
imagePing: imagePing
imagePing: imagePing,
sendHostname: sendHostname
};
30 changes: 30 additions & 0 deletions test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,34 @@ describe('module:request', function() {
expect(trackingElement.src).toBe('https://www.google-analytics.com/collect?v=1&t=event&tid=tracnkingid&cid=cid&dp=dp&dh=dh');
});
});

describe('sendHostname', 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 call appendChild', function(done) {
request.sendHostname('editor');

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

it('should not call appendChild', function(done) {
window.tui.usageStatistics = false;

request.sendHostname('editor');

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