From 4731e585b1811a2355975c514c323c971a394f1e Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 13:35:02 +0300 Subject: [PATCH 01/14] initialize intercom with segment anonymous_id fix #30 --- lib/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index b4c614f..685bd01 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,6 +37,12 @@ var Intercom = module.exports = integration('Intercom') Intercom.prototype.initialize = function() { // Shim out the Intercom library. window.Intercom = function() { + if (window.analytics && + window.analytics.user() && + window.analytics.user().anonymousId()) { + arguments[1]['anonymous_id'] = window.analytics.user().anonymousId(); + } + window.Intercom.q.push(arguments); }; window.Intercom.q = []; @@ -200,7 +206,7 @@ Intercom.prototype.bootOrUpdate = function(options, integrationSettings) { var method = this.booted === true ? 'update' : 'boot'; var activator = this.options.activator; options.app_id = this.options.appId; - + // Intercom, will force the widget to appear if the selector is // #IntercomDefaultWidget so no need to check inbox, just need to check that // the selector isn't #IntercomDefaultWidget. From 3c22904575cab04287b5c71ed6c081aa032f96e6 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:33:35 +0300 Subject: [PATCH 02/14] fix for identified users --- lib/index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 685bd01..ea64b02 100644 --- a/lib/index.js +++ b/lib/index.js @@ -38,9 +38,23 @@ Intercom.prototype.initialize = function() { // Shim out the Intercom library. window.Intercom = function() { if (window.analytics && - window.analytics.user() && - window.analytics.user().anonymousId()) { - arguments[1]['anonymous_id'] = window.analytics.user().anonymousId(); + window.analytics.user()) { + let anonymousId = window.analytics.user().anonymousId(); + let userId = window.analytics.user().id(); + let traits = window.analytics.user().traits(); + + if (anonymousId) { + arguments[1]['anonymous_id'] = anonymousId; + } + + if (userId) { + arguments[1]['id'] = userId; + arguments[1]['user_id'] = userId; + } + + if (traits && Object.keys(traits).length > 0) { + arguments[1] = Object.assign({},arguments[1], traits); + } } window.Intercom.q.push(arguments); From eec956e6cacde5dd0c041e2a1f4d2f3433db355a Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:34:15 +0300 Subject: [PATCH 03/14] Update index.js --- lib/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index ea64b02..254422d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -220,7 +220,6 @@ Intercom.prototype.bootOrUpdate = function(options, integrationSettings) { var method = this.booted === true ? 'update' : 'boot'; var activator = this.options.activator; options.app_id = this.options.appId; - // Intercom, will force the widget to appear if the selector is // #IntercomDefaultWidget so no need to check inbox, just need to check that // the selector isn't #IntercomDefaultWidget. From cf20b6f8c5143ee072eb9594eae74b5d8ca0efd5 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:35:10 +0300 Subject: [PATCH 04/14] Update index.js --- lib/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/index.js b/lib/index.js index 254422d..ea64b02 100644 --- a/lib/index.js +++ b/lib/index.js @@ -220,6 +220,7 @@ Intercom.prototype.bootOrUpdate = function(options, integrationSettings) { var method = this.booted === true ? 'update' : 'boot'; var activator = this.options.activator; options.app_id = this.options.appId; + // Intercom, will force the widget to appear if the selector is // #IntercomDefaultWidget so no need to check inbox, just need to check that // the selector isn't #IntercomDefaultWidget. From 4c4b8ca4efe52f33a061cefee97ef47f0bb71429 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:35:28 +0300 Subject: [PATCH 05/14] Update index.js --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index ea64b02..9ef34e8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -220,7 +220,7 @@ Intercom.prototype.bootOrUpdate = function(options, integrationSettings) { var method = this.booted === true ? 'update' : 'boot'; var activator = this.options.activator; options.app_id = this.options.appId; - + // Intercom, will force the widget to appear if the selector is // #IntercomDefaultWidget so no need to check inbox, just need to check that // the selector isn't #IntercomDefaultWidget. From 863bc9fab9c96ff30885e333f5bafb67f5eb4631 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:40:58 +0300 Subject: [PATCH 06/14] fix tests --- lib/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9ef34e8..0a04c80 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,9 +39,9 @@ Intercom.prototype.initialize = function() { window.Intercom = function() { if (window.analytics && window.analytics.user()) { - let anonymousId = window.analytics.user().anonymousId(); - let userId = window.analytics.user().id(); - let traits = window.analytics.user().traits(); + var anonymousId = window.analytics.user().anonymousId(); + var userId = window.analytics.user().id(); + var traits = window.analytics.user().traits(); if (anonymousId) { arguments[1]['anonymous_id'] = anonymousId; From 087b0a17301f5c620b605ff16dda71054603f7c8 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 14:51:32 +0300 Subject: [PATCH 07/14] fix tests --- lib/index.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0a04c80..21c42bf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,24 +37,24 @@ var Intercom = module.exports = integration('Intercom') Intercom.prototype.initialize = function() { // Shim out the Intercom library. window.Intercom = function() { - if (window.analytics && - window.analytics.user()) { - var anonymousId = window.analytics.user().anonymousId(); - var userId = window.analytics.user().id(); - var traits = window.analytics.user().traits(); - - if (anonymousId) { - arguments[1]['anonymous_id'] = anonymousId; - } - - if (userId) { - arguments[1]['id'] = userId; - arguments[1]['user_id'] = userId; - } - - if (traits && Object.keys(traits).length > 0) { - arguments[1] = Object.assign({},arguments[1], traits); - } + if (window.analytics + && window.analytics.user()) { + var anonymousId = window.analytics.user().anonymousId(); + var userId = window.analytics.user().id(); + var traits = window.analytics.user().traits(); + + if (anonymousId) { + arguments[1].anonymous_id = anonymousId; + } + + if (userId) { + arguments[1].id = userId; + arguments[1].user_id = userId; + } + + if (traits && Object.keys(traits).length > 0) { + arguments[1] = Object.assign({},arguments[1], traits); + } } window.Intercom.q.push(arguments); From f3eac91bd0c80b474a36fce9c4c3d45e0dec7c07 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 16:24:58 +0300 Subject: [PATCH 08/14] CHANGE init place change init of persist tratis to be on the boot page call --- lib/index.js | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/index.js b/lib/index.js index 21c42bf..163d7ec 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,26 +37,6 @@ var Intercom = module.exports = integration('Intercom') Intercom.prototype.initialize = function() { // Shim out the Intercom library. window.Intercom = function() { - if (window.analytics - && window.analytics.user()) { - var anonymousId = window.analytics.user().anonymousId(); - var userId = window.analytics.user().id(); - var traits = window.analytics.user().traits(); - - if (anonymousId) { - arguments[1].anonymous_id = anonymousId; - } - - if (userId) { - arguments[1].id = userId; - arguments[1].user_id = userId; - } - - if (traits && Object.keys(traits).length > 0) { - arguments[1] = Object.assign({},arguments[1], traits); - } - } - window.Intercom.q.push(arguments); }; window.Intercom.q = []; @@ -84,7 +64,33 @@ Intercom.prototype.loaded = function() { Intercom.prototype.page = function(page) { var integrationSettings = page.options(this.name); - this.bootOrUpdate({}, integrationSettings); + + var method = this.booted === true ? 'update' : 'boot'; + + var traits = {} + if (method === 'boot') { + if (window.analytics + && window.analytics.user()) { + var anonymousId = window.analytics.user().anonymousId(); + var userId = window.analytics.user().id(); + var userTraits = window.analytics.user().traits(); + + if (anonymousId) { + traits.anonymous_id = anonymousId; + } + + if (userId) { + traits.id = userId; + traits.user_id = userId; + } + + if (userTraits && Object.keys(userTraits).length > 0) { + traits = Object.assign({},traits, userTraits); + } + } + } + + this.bootOrUpdate(traits, integrationSettings); }; /** From e3394d6fcbc6d4cd94a723712c6d5f93b96af7cf Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 16:28:05 +0300 Subject: [PATCH 09/14] fix tests --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 163d7ec..7c8f522 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,7 +67,7 @@ Intercom.prototype.page = function(page) { var method = this.booted === true ? 'update' : 'boot'; - var traits = {} + var traits = {}; if (method === 'boot') { if (window.analytics && window.analytics.user()) { From 538931f645ac97389f88334fb2f0b5b921768d32 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 17:41:54 +0300 Subject: [PATCH 10/14] change to every page call include new boot mode --- lib/index.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7c8f522..7f9a0b4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -65,28 +65,28 @@ Intercom.prototype.loaded = function() { Intercom.prototype.page = function(page) { var integrationSettings = page.options(this.name); - var method = this.booted === true ? 'update' : 'boot'; + var method = (this.booted === true && page.obj.name !== '_boot') ? 'update' : 'boot'; + var traits = {}; - if (method === 'boot') { - if (window.analytics - && window.analytics.user()) { - var anonymousId = window.analytics.user().anonymousId(); - var userId = window.analytics.user().id(); - var userTraits = window.analytics.user().traits(); - - if (anonymousId) { - traits.anonymous_id = anonymousId; - } + + if (window.analytics + && window.analytics.user()) { + var anonymousId = window.analytics.user().anonymousId(); + var userId = window.analytics.user().id(); + var userTraits = window.analytics.user().traits(); + + if (anonymousId) { + traits.anonymous_id = anonymousId; + } - if (userId) { - traits.id = userId; - traits.user_id = userId; - } + if (userId) { + traits.id = userId; + traits.user_id = userId; + } - if (userTraits && Object.keys(userTraits).length > 0) { - traits = Object.assign({},traits, userTraits); - } + if (userTraits && Object.keys(userTraits).length > 0) { + traits = Object.assign({},traits, userTraits); } } From 269b380347694812cface6e22190da3db59f1eb5 Mon Sep 17 00:00:00 2001 From: Rahav Lussto Date: Mon, 30 Apr 2018 17:50:59 +0300 Subject: [PATCH 11/14] Update index.js --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7f9a0b4..b67fdc4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -65,8 +65,9 @@ Intercom.prototype.loaded = function() { Intercom.prototype.page = function(page) { var integrationSettings = page.options(this.name); - var method = (this.booted === true && page.obj.name !== '_boot') ? 'update' : 'boot'; - + if (page.obj.name === '_boot') { + this.booted = false; + } var traits = {}; From 5d15d8be27af96fd2b1b214e2d46c3f4dbefc15d Mon Sep 17 00:00:00 2001 From: Rahav lussato Date: Wed, 13 Jun 2018 17:53:59 +0300 Subject: [PATCH 12/14] add tests --- lib/index.js | 13 ++++++------ test/index.test.js | 50 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index b67fdc4..eeb1857 100644 --- a/lib/index.js +++ b/lib/index.js @@ -70,12 +70,13 @@ Intercom.prototype.page = function(page) { } var traits = {}; - - if (window.analytics - && window.analytics.user()) { - var anonymousId = window.analytics.user().anonymousId(); - var userId = window.analytics.user().id(); - var userTraits = window.analytics.user().traits(); + + if (this.analytics + && this.analytics.user + && this.analytics.user()) { + var anonymousId = this.analytics.user().anonymousId(); + var userId = this.analytics.user().id(); + var userTraits = this.analytics.user().traits(); if (anonymousId) { traits.anonymous_id = anonymousId; diff --git a/test/index.test.js b/test/index.test.js index d0c4829..2ed8168 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -26,6 +26,7 @@ describe('Intercom', function() { analytics.use(Intercom); analytics.use(tester); analytics.add(intercom); + window.analytics = undefined; }); afterEach(function(done) { @@ -86,12 +87,54 @@ describe('Intercom', function() { it('should call boot first and update subsequently', function() { analytics.page(); analytics.called(window.Intercom, 'boot', { - app_id: options.appId + app_id: options.appId, + anonymous_id: analytics.user().anonymousId() + }); + + analytics.page(); + analytics.called(window.Intercom, 'update', { + app_id: options.appId, + anonymous_id: analytics.user().anonymousId() + }); + }); + + it('should call boot if used with _boot', function() { + analytics.page(); + analytics.called(window.Intercom, 'boot', { + app_id: options.appId, + anonymous_id: analytics.user().anonymousId() }); analytics.page(); analytics.called(window.Intercom, 'update', { - app_id: options.appId + app_id: options.appId, + anonymous_id: analytics.user().anonymousId() + }); + + analytics.page('_boot'); + analytics.calledThrice(window.Intercom, 'boot', { + app_id: options.appId, + anonymous_id: analytics.user().anonymousId() + }); + }); + + describe('page after identify', function() { + beforeEach(function () { + analytics.stub(analytics, 'user', function () { return { + id:function () { return 'id'}, + anonymousId:function () { return 'anonymousId'}, + traits:function () { return undefined} + }}); + }); + + it('should send an id', function() { + analytics.page(); + analytics.called(window.Intercom, 'boot', { + app_id: options.appId, + id: 'id', + user_id: 'id', + anonymous_id: "anonymousId" + }); }); }); }); @@ -462,7 +505,8 @@ describe('Intercom', function() { analytics.page({}, integrationSettings); analytics.called(window.Intercom, 'boot', { app_id: options.appId, - hide_default_launcher: true + hide_default_launcher: true, + anonymous_id: analytics.user().anonymousId() }); }); From 7f5698f4e8ce5782d8d272332ee080cf0e85bd93 Mon Sep 17 00:00:00 2001 From: Rahav lussato Date: Wed, 13 Jun 2018 17:59:17 +0300 Subject: [PATCH 13/14] fix lint --- test/index.test.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 2ed8168..854dfad 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -119,22 +119,24 @@ describe('Intercom', function() { }); describe('page after identify', function() { - beforeEach(function () { - analytics.stub(analytics, 'user', function () { return { - id:function () { return 'id'}, - anonymousId:function () { return 'anonymousId'}, - traits:function () { return undefined} - }}); + beforeEach(function() { + analytics.stub(analytics, 'user', function() { + return { + id:function() { return 'id';}, + anonymousId:function() { return 'anonymousId';}, + traits:function() { return undefined;} + }; + }); }); it('should send an id', function() { - analytics.page(); - analytics.called(window.Intercom, 'boot', { - app_id: options.appId, - id: 'id', - user_id: 'id', - anonymous_id: "anonymousId" - }); + analytics.page(); + analytics.called(window.Intercom, 'boot', { + app_id: options.appId, + id: 'id', + user_id: 'id', + anonymous_id: 'anonymousId' + }); }); }); }); From 3ee85b5b7798e3be5352e548843ecb689c4f2f2a Mon Sep 17 00:00:00 2001 From: Rahav lussato Date: Wed, 13 Jun 2018 18:10:18 +0300 Subject: [PATCH 14/14] remove undefined --- test/index.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index 854dfad..2ae56d5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -26,7 +26,6 @@ describe('Intercom', function() { analytics.use(Intercom); analytics.use(tester); analytics.add(intercom); - window.analytics = undefined; }); afterEach(function(done) {