Skip to content

Commit d863e6a

Browse files
kyuwoo-choikyuwoo.choi
authored andcommitted
feat: add sendHostname (close #16) (#17)
* feat: add sendHostname * fix: apply code review (ref #17)
1 parent bc91519 commit d863e6a

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/js/request.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@
77

88
var object = require('./object');
99
var collection = require('./collection');
10+
var type = require('./type');
11+
12+
/**
13+
* Send hostname on DOMContentLoaded.
14+
* To prevent hostname set tui.usageStatistics to false.
15+
* @param {string} applicationId - application id to send
16+
* @ignore
17+
*/
18+
function sendHostname(applicationId) {
19+
var url = 'https://www.google-analytics.com/collect';
20+
var hostname = location.hostname;
21+
var hitType = 'event';
22+
var trackingId = 'UA-115377265-9';
23+
24+
// skip only if the flag is defined and is set to false explicitly
25+
if (!type.isUndefined(window.tui) && window.tui.usageStatistics === false) {
26+
return;
27+
}
28+
29+
setTimeout(function() {
30+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
31+
imagePing(url, {
32+
v: 1,
33+
t: hitType,
34+
tid: trackingId,
35+
cid: hostname,
36+
dp: hostname,
37+
dh: applicationId
38+
});
39+
}
40+
}, 1000);
41+
}
1042

1143
/**
1244
* Request image ping.
@@ -47,5 +79,6 @@ function imagePing(url, trackingInfo) {
4779
}
4880

4981
module.exports = {
50-
imagePing: imagePing
82+
imagePing: imagePing,
83+
sendHostname: sendHostname
5184
};

test/request.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,34 @@ describe('module:request', function() {
2121
expect(trackingElement.src).toBe('https://www.google-analytics.com/collect?v=1&t=event&tid=tracnkingid&cid=cid&dp=dp&dh=dh');
2222
});
2323
});
24+
25+
describe('sendHostname', function() {
26+
beforeEach(function() {
27+
window.tui = window.tui || {};
28+
29+
// can not spy on imagePing. spy on appendChild instead.
30+
spyOn(document.body, 'appendChild');
31+
spyOn(document.body, 'removeChild');
32+
});
33+
34+
it('should call appendChild', function(done) {
35+
request.sendHostname('editor');
36+
37+
setTimeout(function() {
38+
expect(document.body.appendChild).toHaveBeenCalled();
39+
done();
40+
}, 1500);
41+
});
42+
43+
it('should not call appendChild', function(done) {
44+
window.tui.usageStatistics = false;
45+
46+
request.sendHostname('editor');
47+
48+
setTimeout(function() {
49+
expect(document.body.appendChild).not.toHaveBeenCalled();
50+
done();
51+
}, 1500);
52+
});
53+
});
2454
});

0 commit comments

Comments
 (0)