Skip to content

Commit 70292ea

Browse files
author
Nathan Houle
committed
Merge pull request #638 from segmentio/VentureCraft-master
Initial Elevio integration added
2 parents 0d89969 + c33c010 commit 70292ea

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

integrations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = [
2626
require('./lib/curebit'),
2727
require('./lib/customerio'),
2828
require('./lib/drip'),
29+
require('./lib/elevio'),
2930
require('./lib/errorception'),
3031
require('./lib/evergage'),
3132
require('./lib/extole'),

lib/elevio/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var integration = require('analytics.js-integration');
2+
var tick = require('next-tick');
3+
4+
/**
5+
* Expose `Elevio` integration.
6+
*/
7+
8+
var Elevio = module.exports = integration('Elevio')
9+
.assumesPageview()
10+
.option('accountId', '')
11+
.global('_elev')
12+
.tag('<script src="//static.elev.io/js/v3.js">');
13+
14+
/**
15+
* Initialize elevio.
16+
*/
17+
18+
Elevio.prototype.initialize = function(){
19+
var self = this;
20+
window._elev = window._elev || {};
21+
window._elev.account_id = this.options.accountId;
22+
window._elev.segment = true;
23+
this.load(function(){
24+
tick(self.ready);
25+
});
26+
};
27+
28+
/**
29+
* Has the elevio library been loaded yet?
30+
*
31+
* @return {Boolean}
32+
*/
33+
34+
Elevio.prototype.loaded = function(){
35+
return !!window._elev;
36+
};
37+
38+
/**
39+
* Identify a user.
40+
*
41+
* @param {Facade} identify
42+
*/
43+
44+
Elevio.prototype.identify = function(identify){
45+
var name = identify.name();
46+
var email = identify.email();
47+
var plan = identify.proxy('traits.plan');
48+
49+
var user = {};
50+
user.via = 'segment';
51+
if (email) user.email = email;
52+
if (name) user.name = name;
53+
if (plan) user.plan = [plan];
54+
55+
window._elev.user = user;
56+
};

lib/elevio/test.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var Analytics = require('analytics.js').constructor;
2+
var integration = require('analytics.js-integration');
3+
var tester = require('analytics.js-integration-tester');
4+
var sandbox = require('clear-env');
5+
var Elevio = require('./');
6+
7+
8+
describe('Elevio', function(){
9+
var analytics;
10+
var elevio;
11+
var options = {
12+
accountId: 'konami'
13+
};
14+
15+
beforeEach(function(){
16+
analytics = new Analytics();
17+
elevio = new Elevio(options);
18+
analytics.use(Elevio);
19+
analytics.use(tester);
20+
analytics.add(elevio);
21+
});
22+
23+
afterEach(function(){
24+
analytics.restore();
25+
analytics.reset();
26+
elevio.reset();
27+
sandbox();
28+
});
29+
30+
it('should have the right settings', function(){
31+
analytics.compare(Elevio, integration('Elevio')
32+
.assumesPageview()
33+
.global('_elev')
34+
.option('accountId', ''));
35+
});
36+
37+
describe('before loading', function(){
38+
beforeEach(function(){
39+
analytics.stub(window, '_elev');
40+
analytics.stub(elevio, 'load');
41+
});
42+
describe('#initialize', function(){
43+
it('should create window._elev', function(){
44+
analytics.initialize();
45+
analytics.page();
46+
analytics.assert(window._elev instanceof Object);
47+
});
48+
it('should call #load', function(){
49+
analytics.initialize();
50+
analytics.page();
51+
analytics.called(elevio.load);
52+
});
53+
});
54+
});
55+
56+
describe('loading', function(){
57+
it('should load', function(done){
58+
analytics.load(elevio, done);
59+
});
60+
});
61+
62+
describe('after loading', function(){
63+
beforeEach(function(done){
64+
analytics.once('ready', done);
65+
analytics.initialize();
66+
analytics.page();
67+
});
68+
69+
describe('#identify', function(){
70+
beforeEach(function(){
71+
// TODO: stub the integration global api.
72+
// For example:
73+
// analytics.stub(window.api, 'identify');
74+
});
75+
76+
it('should send an email', function(){
77+
analytics.identify('id', { email: '[email protected]' });
78+
analytics.assert(window._elev.user.email === '[email protected]');
79+
});
80+
81+
it('should send a full name', function(){
82+
analytics.identify('id', { name: 'Test Person' });
83+
analytics.assert(window._elev.user.name === 'Test Person');
84+
});
85+
86+
it('should not send name', function(){
87+
analytics.identify('id');
88+
analytics.assert(window._elev.user.name === undefined);
89+
});
90+
91+
// TODO seems Identify.prototype.name returns nothing when no last name is sent
92+
// it('should send a first name', function(){
93+
// analytics.identify(undefined, { firstName: 'Test' });
94+
// analytics.assert(window._elev.user.name === 'Test');
95+
// });
96+
97+
it('should send a combined name', function(){
98+
analytics.identify('id', { firstName: 'Test', lastName: 'Person' });
99+
analytics.assert(window._elev.user.name === 'Test Person');
100+
});
101+
102+
it('should send their plan', function(){
103+
analytics.identify('id', { plan: 'gold' });
104+
analytics.assert(window._elev.user.plan instanceof Array);
105+
analytics.assert(window._elev.user.plan[0] === 'gold');
106+
});
107+
});
108+
});
109+
});

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(85, object.length(Integrations));
23+
assert.equal(86, object.length(Integrations));
2424
});
2525
});
2626

0 commit comments

Comments
 (0)