Skip to content

Commit bea65a6

Browse files
committed
Add Scratch-it Analytics integration [wip]
1 parent 7e7cd49 commit bea65a6

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

integrations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = [
7373
require('./lib/route'),
7474
require('./lib/saasquatch'),
7575
require('./lib/satismeter'),
76+
require('./lib/scratch-it'),
7677
require('./lib/segmentio'),
7778
require('./lib/sentry'),
7879
require('./lib/snapengage'),

lib/scratch-it/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var integration = require('analytics.js-integration');
7+
var is = require('is');
8+
var del = require('obj-case').del;
9+
10+
/**
11+
* Expose `ScratchIt` integration.
12+
*/
13+
14+
var ScratchIt = module.exports = integration('Scratch-it Analytics')
15+
.global('ScratchIt')
16+
.option('trkId', '')
17+
.tag('<script src="//static.scratch-it.com/public/scratch-it-analytics.min.js">');
18+
19+
/**
20+
* Initialize.
21+
*/
22+
23+
ScratchIt.prototype.initialize = function() {
24+
// Shim out the Scratchit library.
25+
window.ScratchIt = {
26+
track: function(event_type, event_name, parameters) {
27+
// no-op
28+
console.log(event_type);
29+
console.log(event_name);
30+
console.log(parameters);
31+
}
32+
}; // TODO: real object
33+
this.load(this.ready);
34+
};
35+
36+
/**
37+
* Loaded?
38+
*
39+
* @return {boolean}
40+
*/
41+
42+
ScratchIt.prototype.loaded = function() {
43+
return is.fn(window.ScratchIt);
44+
};
45+
46+
/**
47+
* Track.
48+
*
49+
* @param {Track} track
50+
*/
51+
52+
ScratchIt.prototype.track = function(track) {
53+
var parameters = track.properties();
54+
var event_type = parameters.event_type || 'track';
55+
del(parameters, 'event_type');
56+
window.ScratchIt.track(event_type, track.event(), parameters);
57+
};

lib/scratch-it/test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var Analytics = require('analytics.js').constructor;
2+
var integration = require('analytics.js-integration');
3+
var sandbox = require('clear-env');
4+
var tester = require('analytics.js-integration-tester');
5+
var ScratchIt = require('./');
6+
7+
describe('ScratchIt', function() {
8+
var analytics;
9+
var scratchIt;
10+
var options = {
11+
trkId: 'foo'
12+
};
13+
14+
beforeEach(function() {
15+
analytics = new Analytics();
16+
scratchIt = new ScratchIt(options);
17+
analytics.use(ScratchIt);
18+
analytics.use(tester);
19+
analytics.add(scratchIt);
20+
});
21+
22+
afterEach(function() {
23+
analytics.restore();
24+
analytics.reset();
25+
scratchIt.reset();
26+
sandbox();
27+
});
28+
29+
it('should have the right settings', function() {
30+
analytics.compare(ScratchIt, integration('Scratch-it Analytics')
31+
.global('ScratchIt')
32+
.option('trkId', ''));
33+
});
34+
35+
describe('before loading', function() {
36+
beforeEach(function() {
37+
analytics.stub(scratchIt, 'load');
38+
});
39+
40+
describe('#initialize', function() {
41+
it('should create the window.ScratchIt object', function() {
42+
analytics.assert(window.ScratchIt === undefined);
43+
analytics.initialize();
44+
analytics.assert(window.ScratchIt);
45+
});
46+
47+
it('should call #load', function() {
48+
analytics.initialize();
49+
analytics.called(scratchIt.load);
50+
});
51+
});
52+
});
53+
54+
describe('after loading', function() {
55+
beforeEach(function(done) {
56+
analytics.once('ready', done);
57+
analytics.initialize();
58+
});
59+
60+
describe('#track', function() {
61+
beforeEach(function() {
62+
analytics.stub(window.ScratchIt, 'track');
63+
});
64+
65+
it('should send an event with default type', function() {
66+
analytics.track('Goal Reached');
67+
analytics.called(window.ScratchIt.track, 'track', 'Goal Reached', {});
68+
});
69+
70+
it('should send an event', function() {
71+
analytics.track('Started Scratching', { event_type: 'scratching_page' });
72+
analytics.called(window.ScratchIt.track, 'scratching_page', 'Started Scratching', {});
73+
});
74+
75+
it('should send an event with properties', function() {
76+
analytics.track('Started Scratching', { event_type: 'scratching_page', foo: 'bar' });
77+
analytics.called(window.ScratchIt.track, 'scratching_page', 'Started Scratching', { foo: 'bar' });
78+
});
79+
});
80+
});
81+
});

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mocha.setup({
2020

2121
describe('integrations', function(){
2222
it('should export our integrations', function(){
23-
assert.equal(86, object.length(Integrations));
23+
assert.equal(87, object.length(Integrations));
2424
});
2525
});
2626

0 commit comments

Comments
 (0)