Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 3098256

Browse files
author
Han Kim
committed
support hide default launcher
1 parent 19cae57 commit 3098256

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

lib/index.js

+33-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var defaults = require('@ndhoule/defaults');
99
var del = require('obj-case').del;
1010
var integration = require('@segment/analytics.js-integration');
1111
var is = require('is-type');
12+
var extend = require('@ndhoule/extend');
1213

1314
/**
1415
* Expose `Intercom` integration.
@@ -57,8 +58,9 @@ Intercom.prototype.loaded = function() {
5758
* @param {Page} page
5859
*/
5960

60-
Intercom.prototype.page = function() {
61-
this.bootOrUpdate();
61+
Intercom.prototype.page = function(page) {
62+
var integrationSettings = page.options(this.name);
63+
this.bootOrUpdate({}, integrationSettings);
6264
};
6365

6466
/**
@@ -72,14 +74,14 @@ Intercom.prototype.page = function() {
7274

7375
Intercom.prototype.identify = function(identify) {
7476
var traits = identify.traits({ userId: 'user_id' });
75-
var opts = identify.options(this.name);
77+
var integrationSettings = identify.options(this.name);
7678
var companyCreated = identify.companyCreated();
7779
var created = identify.created();
7880
var name = identify.name();
7981
var id = identify.userId();
8082
var group = this.analytics.group();
8183

82-
if (!id && !traits.email) {
84+
if (!id && !identify.email()) {
8385
return;
8486
}
8587

@@ -113,10 +115,10 @@ Intercom.prototype.identify = function(identify) {
113115
traits = convertDates(traits, formatDate);
114116

115117
// handle options
116-
if (opts.userHash) traits.user_hash = opts.userHash;
117-
if (opts.user_hash) traits.user_hash = opts.user_hash;
118+
if (integrationSettings.userHash) traits.user_hash = integrationSettings.userHash;
119+
if (integrationSettings.user_hash) traits.user_hash = integrationSettings.user_hash;
118120

119-
this.bootOrUpdate(traits);
121+
this.bootOrUpdate(traits, integrationSettings);
120122
};
121123

122124
/**
@@ -135,7 +137,10 @@ Intercom.prototype.group = function(group) {
135137
props = convertDates(props, formatDate);
136138
var id = group.groupId();
137139
if (id) props.id = id;
138-
api('update', { company: props });
140+
var integrationSettings = group.options(this.name);
141+
var traits = extend({ company: props }, hideDefaultLauncher(integrationSettings));
142+
143+
api('update', traits);
139144
};
140145

141146
/**
@@ -156,7 +161,7 @@ Intercom.prototype.track = function(track) {
156161
* @param {Object} options
157162
*/
158163

159-
Intercom.prototype.bootOrUpdate = function(options) {
164+
Intercom.prototype.bootOrUpdate = function(options, integrationSettings) {
160165
options = options || {};
161166
var method = this.booted === true ? 'update' : 'boot';
162167
var activator = this.options.activator;
@@ -168,6 +173,8 @@ Intercom.prototype.bootOrUpdate = function(options) {
168173
if (activator !== '#IntercomDefaultWidget') {
169174
options.widget = { activator: activator };
170175
}
176+
// Check for selective showing of messenger option
177+
options = extend(options, hideDefaultLauncher(integrationSettings));
171178

172179
api(method, options);
173180
this.booted = true;
@@ -194,3 +201,20 @@ function formatDate(date) {
194201
function api() {
195202
window.Intercom.apply(window.Intercom, arguments);
196203
}
204+
205+
/**
206+
* Selectively hide messenger
207+
* https://docs.intercom.io/configure-intercom-for-your-product-or-site/customize-the-intercom-messenger/customize-the-intercom-messenger-technical#show-the-intercom-messenger-to-selected-users-for-web-
208+
* @param {Object} options
209+
* @return {Object} ret
210+
* @api private
211+
*/
212+
213+
function hideDefaultLauncher(options) {
214+
var ret = {};
215+
var setting = options.hideDefaultLauncher;
216+
if (setting === undefined || typeof setting !== 'boolean') return ret;
217+
ret.hide_default_launcher= setting;
218+
return ret;
219+
}
220+

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"homepage": "https://github.com/segment-integrations/analytics.js-integration-intercom#readme",
2525
"dependencies": {
2626
"@ndhoule/defaults": "^2.0.1",
27+
"@ndhoule/extend": "^2.0.0",
2728
"@segment/analytics.js-integration": "^2.1.0",
2829
"@segment/convert-dates": "^1.0.0",
2930
"is-type": "0.0.1",

test/index.test.js

+41
Original file line numberDiff line numberDiff line change
@@ -349,5 +349,46 @@ describe('Intercom', function() {
349349
analytics.called(window.Intercom, 'trackEvent', 'event', {});
350350
});
351351
});
352+
353+
describe('integration settings', function() {
354+
beforeEach(function() {
355+
analytics.stub(window, 'Intercom');
356+
});
357+
358+
it('page: should set hide_default_launcher if integration setting exists for it', function() {
359+
var integrationSettings = {
360+
Intercom: { hideDefaultLauncher: true }
361+
};
362+
analytics.page({}, integrationSettings);
363+
analytics.called(window.Intercom, 'boot', {
364+
app_id: options.appId,
365+
hide_default_launcher: true
366+
});
367+
});
368+
369+
it('identify: should set hide_default_launcher if integration setting exists for it', function() {
370+
var integrationSettings = {
371+
Intercom: { hideDefaultLauncher: true }
372+
};
373+
analytics.identify('id', {}, integrationSettings);
374+
analytics.called(window.Intercom, 'boot', {
375+
app_id: options.appId,
376+
user_id: 'id',
377+
id: 'id',
378+
hide_default_launcher: true
379+
});
380+
});
381+
382+
it('group: should set hide_default_launcher if integration setting exists for it', function() {
383+
var integrationSettings = {
384+
Intercom: { hideDefaultLauncher: true }
385+
};
386+
analytics.group('id', {}, integrationSettings);
387+
analytics.called(window.Intercom, 'update', {
388+
company: { id: 'id' },
389+
hide_default_launcher: true
390+
});
391+
});
392+
});
352393
});
353394
});

0 commit comments

Comments
 (0)