Skip to content

Commit

Permalink
feat: send hostname (#16)
Browse files Browse the repository at this point in the history
* feat: send hostname

* test: add test for usage statistics
  • Loading branch information
kyuwoo-choi committed Apr 12, 2018
1 parent eff3be2 commit c50cd89
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -190,7 +190,7 @@ You can also see the older versions of API page on the [releases page](https://g


## 🔩 Dependency
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.2.5
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.3.0


## 📜 License
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Expand Up @@ -20,6 +20,6 @@
"package.json"
],
"dependencies": {
"tui-code-snippet": "^1.2.5"
"tui-code-snippet": "^1.3.0"
}
}
2 changes: 1 addition & 1 deletion examples/example01-basic.html
Expand Up @@ -62,7 +62,7 @@ <h3>easeInOutQuint</h3>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.min.js"></script>
<script src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script src="../dist/tui-animation.js"></script>
<script class="code-js">
function run(container) {
Expand Down
2 changes: 1 addition & 1 deletion examples/example02-2D-movement.html
Expand Up @@ -28,7 +28,7 @@
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.min.js"></script>
<script src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script src="../dist/tui-animation.js"></script>
<script class="code-js">
var container = $('.container');
Expand Down
2 changes: 1 addition & 1 deletion examples/example03-using-promise.html
Expand Up @@ -31,7 +31,7 @@
<div class="move-console"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.min.js"></script>
<script src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script src="../dist/tui-animation.js"></script>
<script class="code-js">
const isSupportPromise = (typeof Promise !== "undefined") && (/\[native code\]/.test(Promise.toString()));
Expand Down
45 changes: 26 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -57,6 +57,6 @@
"webpack-dev-server": "^1.11.0"
},
"dependencies": {
"tui-code-snippet": "^1.2.5"
"tui-code-snippet": "^1.3.0"
}
}
33 changes: 31 additions & 2 deletions src/js/anim.js
Expand Up @@ -10,10 +10,11 @@
*/

import * as easingFunctions from './easing';
import {isArray, map} from 'tui-code-snippet';
import {imagePing, isArray, map} from 'tui-code-snippet';

const isSupportPromise = (typeof Promise !== 'undefined') &&
(/\[native code\]/.test(Promise.toString()));
let hostnameSent = false;

/** Do nothing */
function noop() {}
Expand Down Expand Up @@ -78,6 +79,28 @@ export function cancelAnimFrame(timerId) {
cancelFn(timerId);
}

/**
* send hostname
* @ignore
*/
function sendHostname() {
const {hostname} = location;

if (hostnameSent) {
return;
}
hostnameSent = true;

imagePing('https://www.google-analytics.com/collect', {
v: 1,
t: 'event',
tid: 'UA-115377265-9',
cid: hostname,
dp: hostname,
dh: 'animation'
});
}

/**
* Get animation runner
* @memberof tui.animation
Expand All @@ -90,6 +113,7 @@ export function cancelAnimFrame(timerId) {
* @param {Function} [options.frame] - invoking each frames. you can manipulate specific element by this function
* the arguments passed with same sequence with `from`, `to` option values
* @param {Function} [options.complete] - invoked once at end of animation
* @param {Boolean} [options.usageStatistics=true] - Let us know the hostname. If you don't want to send the hostname, please set to false.
* @returns {Object} animation runner
* @tutorial example01-basic-usage
* @tutorial example02-2D-movement
Expand Down Expand Up @@ -128,7 +152,8 @@ export function anim({
duration = 1000,
easing = 'linear',
frame = noop,
complete = noop
complete = noop,
usageStatistics = true
} = {}) {
from = isArray(from) ? from : [from];
to = isArray(to) ? to : [to];
Expand All @@ -138,6 +163,10 @@ export function anim({

easing = easingFunctions[easing] || easingFunctions.linear;

if (usageStatistics) {
sendHostname();
}

/**
* Get animation runner object
* @param {function} resolve - resolve from promise
Expand Down
21 changes: 21 additions & 0 deletions test/anim.spec.js
@@ -1,4 +1,5 @@
import {anim} from '../src/js/anim';
import snippet from 'tui-code-snippet';

describe('animation', () => {
let box;
Expand Down Expand Up @@ -75,4 +76,24 @@ describe('animation', () => {
}).run();
}).not.toThrow();
});

// hostnameSent module scope variable can not be reset.
// maintain cases with xit as it always fail, if you want to test these cases, change xit to fit one by one
describe('usageStatistics', () => {
beforeEach(() => {
spyOn(snippet, 'imagePing');
});

xit('should send hostname by default', done => {
anim({duration: 0, complete: done}).run();

expect(snippet.imagePing).toHaveBeenCalled();
});

xit('should not send hostname on usageStatistics option false', done => {
anim({duration: 0, complete: done, usageStatistics: false}).run();

expect(snippet.imagePing).not.toHaveBeenCalled();
});
});
});

0 comments on commit c50cd89

Please sign in to comment.