Skip to content

Commit 310353f

Browse files
author
Julio Farah
authored
Add Crisp integration (#619)
* Add Crisp integration
1 parent c1b2522 commit 310353f

File tree

9 files changed

+1431
-37
lines changed

9 files changed

+1431
-37
lines changed

integrations/crisp/HISTORY.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
3.0.1 / 2018-07-18
2+
==================
3+
* Supports mapping to settings.sourceName on `track` and `page` calls (#32)
4+
* Fix flaky tests (#31)
5+
6+
3.0.0 / 2017-10-19
7+
==================
8+
9+
* Parity rewrite (#29)
10+
* Bump a.js-int-tester version to ^3.1.0 (#28)
11+
* Pin karma, karma-mocha dev dependencies (#27)
12+
13+
2.2.0 / 2016-08-16
14+
==================
15+
16+
* update snippet
17+
18+
2.1.0 / 2016-08-12
19+
==================
20+
21+
* tag mp_lib with Segment: web
22+
23+
2.0.0 / 2016-06-21
24+
==================
25+
26+
* Remove Duo compatibility
27+
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
28+
* Update eslint configuration
29+
30+
31+
1.5.0 / 2016-05-12
32+
==================
33+
34+
* Update Page Call Logic with consolidatedPageCalls setting
35+
36+
1.4.2 / 2016-05-07
37+
==================
38+
39+
* Bump Analytics.js core, tester, integration to use Facade 2.x
40+
41+
1.4.1 / 2016-02-03
42+
==================
43+
44+
* fix multiple page call issue
45+
46+
1.4.0 / 2015-12-09
47+
==================
48+
49+
* add persistence config
50+
51+
1.3.2 / 2015-11-03
52+
==================
53+
54+
* make mixpanel es5 friendly
55+
* Dont set peopleProperties in track calls.
56+
57+
1.3.1 / 2015-10-29
58+
==================
59+
60+
* Dont set peopleProperties in track calls. Dont map special traits in track, people should use identify instead.
61+
62+
1.3.0 / 2015-10-27
63+
==================
64+
65+
66+
67+
1.3.0 / 2015-10-27
68+
==================
69+
70+
* refactor super people props
71+
72+
1.2.1 / 2015-09-08
73+
==================
74+
75+
* adding check for no Mixpanel.superProperties object closes #11
76+
77+
1.2.0 / 2015-09-08
78+
==================
79+
80+
* Allow explicit declaration of people/super properties ([More Info](https://github.com/segment-integrations/analytics.js-integration-mixpanel/pull/10))
81+
* stub every possible mixpanel method
82+
83+
1.1.0 / 2015-08-25
84+
==================
85+
86+
* Merge pull request #6 from segment-integrations/people-super-properties
87+
* added enhancedTrack support
88+
89+
1.0.5 / 2015-08-05
90+
==================
91+
92+
* Send arrays through to Mixpanel
93+
94+
1.0.4 / 2015-06-30
95+
==================
96+
97+
* Replace analytics.js dependency with analytics.js-core
98+
99+
1.0.3 / 2015-06-24
100+
==================
101+
102+
* Bump analytics.js-integration version
103+
104+
1.0.2 / 2015-06-24
105+
==================
106+
107+
* Bump analytics.js-integration version
108+
109+
1.0.1 / 2015-06-10
110+
==================
111+
112+
* Bump dependency versions
113+
114+
1.0.0 / 2015-06-09
115+
==================
116+
117+
* Initial commit :sparkles:

integrations/crisp/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# analytics.js-integration-crisp
2+
3+
Crisp integration for [Analytics.js][].
4+
5+
## License
6+
7+
Released under the [MIT license](LICENSE).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../karma.conf-ci.js');

integrations/crisp/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../karma.conf');

integrations/crisp/lib/index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var integration = require('@segment/analytics.js-integration');
8+
9+
/**
10+
* Expose `Crisp` integration.
11+
*
12+
* https://help.crisp.chat/en/article/how-to-use-dollarcrisp-javascript-sdk-10ud15y/
13+
*/
14+
15+
var Crisp = (module.exports = integration('Crisp')
16+
.global('$crisp')
17+
.option('websiteId', '')
18+
.option('listen', false)
19+
.readyOnLoad());
20+
21+
/**
22+
* Initialize.
23+
*/
24+
Crisp.prototype.initialize = function() {
25+
var self = this;
26+
27+
this.load(function() {
28+
self.ready();
29+
});
30+
};
31+
32+
/**
33+
* Load
34+
*/
35+
Crisp.prototype.load = function(done) {
36+
window.CRISP_WEBSITE_ID = this.options.websiteId;
37+
window.$crisp = [];
38+
39+
var script = document.createElement('script');
40+
script.src = 'https://client.crisp.chat/l.js';
41+
script.async = 1;
42+
document.getElementsByTagName('head')[0].appendChild(script);
43+
44+
window.$crisp.push(['on', 'session:loaded', done]);
45+
};
46+
47+
/**
48+
* Loaded?
49+
*
50+
* @return {Boolean}
51+
*/
52+
Crisp.prototype.loaded = function() {
53+
return (
54+
typeof window.$crisp === 'object' && typeof window.$crisp.is === 'function'
55+
);
56+
};
57+
58+
Crisp.prototype.identify = function(identify) {
59+
if (identify.email()) {
60+
window.$crisp.push(['set', 'user:email', identify.email()]);
61+
}
62+
63+
if (identify.name() || identify.firstName()) {
64+
window.$crisp.push([
65+
'set',
66+
'user:nickname',
67+
identify.name() || identify.firstName()
68+
]);
69+
}
70+
71+
if (identify.phone()) {
72+
window.$crisp.push(['set', 'user:phone', [identify.phone()]]);
73+
}
74+
75+
if (identify.avatar()) {
76+
window.$crisp.push(['set', 'user:avatar', [identify.avatar()]]);
77+
}
78+
};

integrations/crisp/package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "analytics.js-integration-crisp",
3+
"description": "The Crisp analytics.js integration.",
4+
"version": "3.0.0",
5+
"keywords": [
6+
"analytics.js",
7+
"analytics.js-integration",
8+
"segment",
9+
"crisp"
10+
],
11+
"main": "lib/index.js",
12+
"scripts": {
13+
"test": "karma start",
14+
"test:ci": "karma start karma.conf-ci.js"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/segment-integrations/analytics.js-integration-crisp.git"
19+
},
20+
"author": "Segment <friends@segment.com>",
21+
"license": "SEE LICENSE IN LICENSE",
22+
"bugs": {
23+
"url": "https://github.com/segment-integrations/analytics.js-integration-crisp/issues"
24+
},
25+
"homepage": "https://github.com/segment-integrations/analytics.js-integration-crisp#readme",
26+
"dependencies": {
27+
"@segment/analytics.js-integration": "^2.1.0"
28+
},
29+
"devDependencies": {
30+
"@segment/analytics.js-core": "^3.0.0",
31+
"@segment/analytics.js-integration-tester": "^3.1.0",
32+
"@segment/clear-env": "^2.0.0",
33+
"browserify": "^13.0.0",
34+
"eslint": "^5.16.0",
35+
"karma": "1.3.0",
36+
"karma-browserify": "^5.0.4",
37+
"karma-chrome-launcher": "^1.0.1",
38+
"karma-coverage": "^1.0.0",
39+
"karma-junit-reporter": "^1.0.0",
40+
"karma-mocha": "1.0.1",
41+
"karma-phantomjs-launcher": "^1.0.0",
42+
"karma-sauce-launcher": "^1.0.0",
43+
"karma-spec-reporter": "0.0.26",
44+
"mocha": "^2.2.5",
45+
"watchify": "^3.7.0"
46+
}
47+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
'use strict';
2+
3+
var Analytics = require('@segment/analytics.js-core').constructor;
4+
var integration = require('@segment/analytics.js-integration');
5+
var tester = require('@segment/analytics.js-integration-tester');
6+
var Crisp = require('../lib/');
7+
var sandbox = require('@segment/clear-env');
8+
9+
describe('Crisp', function() {
10+
var analytics;
11+
var options;
12+
var crisp;
13+
14+
beforeEach(function() {
15+
options = {
16+
websiteId: '7598bf86-9ebb-46bc-8c61-be8929bbf93d',
17+
listen: true
18+
};
19+
20+
analytics = new Analytics();
21+
crisp = new Crisp(options);
22+
analytics.use(Crisp);
23+
analytics.use(tester);
24+
analytics.add(crisp);
25+
});
26+
27+
afterEach(function() {
28+
analytics.restore();
29+
analytics.reset();
30+
crisp.reset();
31+
sandbox();
32+
});
33+
34+
it('should have the correct settings', function() {
35+
analytics.compare(
36+
Crisp,
37+
integration('Crisp')
38+
.global('$crisp')
39+
.option('websiteId', '')
40+
.readyOnLoad()
41+
);
42+
});
43+
44+
describe('#initialize', function() {
45+
beforeEach(function() {
46+
analytics.stub(crisp, 'load');
47+
});
48+
49+
it('should call #load', function() {
50+
analytics.didNotCall(crisp.load);
51+
crisp.initialize();
52+
analytics.calledOnce(crisp.load);
53+
});
54+
});
55+
56+
describe('#loaded', function() {
57+
it('should return `false` when Crisp is not loaded', function() {
58+
analytics.assert(crisp.loaded() === false);
59+
});
60+
61+
it('should return `true` when Crisp is loaded', function() {
62+
window.$crisp = [];
63+
window.$crisp.is = function() {};
64+
analytics.assert(crisp.loaded() === true);
65+
});
66+
});
67+
68+
xdescribe('before loading', function() {
69+
beforeEach(function() {
70+
analytics.stub(crisp, 'load');
71+
});
72+
});
73+
74+
describe('after loading', function() {
75+
describe('#load', function(done) {
76+
beforeEach(function() {
77+
analytics.stub(crisp, 'load');
78+
});
79+
80+
it('should initialize `window.$crisp`', function() {
81+
analytics.assert(typeof window.$crisp === 'undefined');
82+
83+
crisp.load(function() {
84+
analytics.assert(typeof window.$crisp !== 'undefined');
85+
done();
86+
});
87+
});
88+
});
89+
90+
describe('#identify', function() {
91+
beforeEach(function(done) {
92+
analytics.once('ready', done);
93+
94+
analytics.initialize();
95+
});
96+
97+
it('should set user properties', function() {
98+
var attrs = {
99+
name: 'Bob Loblaw',
100+
email: 'bob.loblaw@test.com',
101+
phone: '555-555-5555'
102+
};
103+
analytics.identify('user-id', attrs);
104+
});
105+
106+
it('should fall back on `firstName` if `lastName` or `name` are not specified', function() {
107+
var attrs = {
108+
firstName: 'Bob',
109+
email: 'bob.loblaw@test.com'
110+
};
111+
analytics.identify('user-id', attrs);
112+
});
113+
});
114+
});
115+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"eslint": "^7.12.1",
2929
"eslint-config-prettier": "^6.14.0",
3030
"eslint-plugin-prettier": "^3.1.4",
31+
"eslint-config-airbnb-base": "14.2.1",
3132
"fs-extra": "^9.0.1",
3233
"husky": "^4.3.8",
3334
"lerna": "^3.15.0",

0 commit comments

Comments
 (0)