From 8baf25b0eb945ad4f18ad5e90fa034b8999a9f55 Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Tue, 6 Dec 2022 11:55:52 -0500 Subject: [PATCH] initialize pendo with current user and group (#707) (#710) --- integrations/pendo/HISTORY.md | 5 +++++ integrations/pendo/lib/index.js | 24 ++++++++++++++++++++ integrations/pendo/package.json | 2 +- integrations/pendo/test/index.test.js | 32 +++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/integrations/pendo/HISTORY.md b/integrations/pendo/HISTORY.md index f9106520c..f76339b29 100644 --- a/integrations/pendo/HISTORY.md +++ b/integrations/pendo/HISTORY.md @@ -1,3 +1,8 @@ +1.1.4 / 2022-11-18 +=================== + + * initialize pendo with current user and group + 1.1.2 / 2020-12-14 =================== diff --git a/integrations/pendo/lib/index.js b/integrations/pendo/lib/index.js index ce778e7d4..cbbeae5d2 100644 --- a/integrations/pendo/lib/index.js +++ b/integrations/pendo/lib/index.js @@ -37,6 +37,30 @@ Pendo.prototype.initialize = function() { usePendoAgentAPI: true }; + var user = this.analytics.user(); + var isUserAnonymous = !user.id(); + var id = isUserAnonymous + ? pendoifyAnonymousId(user.anonymousId()) + : user.id(); + + var visitor = Object.assign({ id: id }, user.traits()); + window.pendo_options.visitor = Object.assign( + visitor, + window.pendo_options.visitor + ); + + var group = this.analytics.group(); + if (group.id()) { + var account = Object.assign( + { id: group.id() }, + group.traits() + ); + window.pendo_options.account = Object.assign( + account, + window.pendo_options.account + ); + } + this.load(this.ready, { apiKey: this.options.apiKey }); }; diff --git a/integrations/pendo/package.json b/integrations/pendo/package.json index 3a992b98a..f5f0ecde4 100644 --- a/integrations/pendo/package.json +++ b/integrations/pendo/package.json @@ -1,7 +1,7 @@ { "name": "@segment/analytics.js-integration-pendo", "description": "The Pendo analytics.js integration.", - "version": "1.1.3", + "version": "1.1.4", "keywords": [ "analytics.js", "analytics.js-integration", diff --git a/integrations/pendo/test/index.test.js b/integrations/pendo/test/index.test.js index 6db8c56c9..e7dea4627 100644 --- a/integrations/pendo/test/index.test.js +++ b/integrations/pendo/test/index.test.js @@ -9,11 +9,12 @@ var tester = require('@segment/analytics.js-integration-tester'); describe('Pendo', function() { var analytics; var pendo; - var options = { - apiKey: 'test-key-for-segment-integration' - }; + var options; beforeEach(function() { + options = { + apiKey: 'test-key-for-segment-integration' + }; analytics = new Analytics(); pendo = new Pendo(options); @@ -24,6 +25,7 @@ describe('Pendo', function() { }); afterEach(function() { + delete window.pendo_options; analytics.restore(); analytics.reset(); pendo.reset(); @@ -60,9 +62,31 @@ describe('Pendo', function() { }); it('should create a pendo_options object using API', function() { + analytics.initialize(); + analytics.assert.deepEqual(window.pendo_options, { + apiKey: options.apiKey, + usePendoAgentAPI: true, + visitor: { + id: '_PENDO_T_' + analytics.user().anonymousId() + } + }); + }); + + it('should create a pendo_options object for a user and group', function() { + analytics.identify('user1', { foo: 'bar' }); + analytics.group('group1', { baz: 'quux' }); + analytics.initialize(); analytics.assert.deepEqual(window.pendo_options, { apiKey: options.apiKey, - usePendoAgentAPI: true + usePendoAgentAPI: true, + visitor: { + id: 'user1', + foo: 'bar' + }, + account: { + id: 'group1', + baz: 'quux' + } }); }); });