Skip to content
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: 34 additions & 1 deletion src/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@

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) {
var url = 'https://www.google-analytics.com/collect';
var hostname = location.hostname;
var hitType = 'event';
var trackingId = 'UA-115377265-9';

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

setTimeout(function() {
if (document.readyState === 'interactive' || document.readyState === 'complete') {
imagePing(url, {
v: 1,
t: hitType,
tid: trackingId,
cid: hostname,
dp: hostname,
dh: applicationId
});
}
}, 100);
}

/**
* Request image ping.
Expand Down Expand Up @@ -47,5 +79,6 @@ function imagePing(url, trackingInfo) {
}

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();
}, 1000);
});

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

request.sendHostname('editor');

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