diff --git a/lib/index.js b/lib/index.js index b4c614f..eeb1857 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,7 +64,35 @@ Intercom.prototype.loaded = function() { Intercom.prototype.page = function(page) { var integrationSettings = page.options(this.name); - this.bootOrUpdate({}, integrationSettings); + + if (page.obj.name === '_boot') { + this.booted = false; + } + + var 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; + } + + 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); }; /** diff --git a/test/index.test.js b/test/index.test.js index d0c4829..2ae56d5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -86,12 +86,56 @@ 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 +506,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() }); });