diff --git a/packages/analytics-js-integrations/__tests__/integrations/Appcues/appcues.test.js b/packages/analytics-js-integrations/__tests__/integrations/Appcues/appcues.test.js index 140f24876e..33a59d4bf7 100644 --- a/packages/analytics-js-integrations/__tests__/integrations/Appcues/appcues.test.js +++ b/packages/analytics-js-integrations/__tests__/integrations/Appcues/appcues.test.js @@ -20,6 +20,7 @@ const mockAppcuesSDK = () => { identify: jest.fn(), track: jest.fn(), page: jest.fn(), + group: jest.fn(), }; }; @@ -161,3 +162,33 @@ describe('Appcues page tests', () => { }); }); }); +describe('Appcues group tests', () => { + let appcues; + const rudderElement = { + message: { + type: 'group', + groupId: 'testid', + traits: { + name: 'test', + }, + }, + }; + it('Group -> Group Id and traits present', () => { + appcues = new Appcues(appcuesConfig, { loglevel: 'debug' }); + appcues.init(); + mockAppcuesSDK(); + const spy = jest.spyOn(window.Appcues, 'group'); + appcues.group(rudderElement); + expect(spy).toHaveBeenCalledWith('testid', { + name: 'test', + }); + }); + it('Group -> Group Id not present', () => { + appcues = new Appcues(appcuesConfig, { loglevel: 'debug' }); + appcues.init(); + mockAppcuesSDK(); + const spy = jest.spyOn(window.Appcues, 'group'); + appcues.group({ message: {} }); + expect(spy).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/analytics-js-integrations/src/integrations/Appcues/browser.js b/packages/analytics-js-integrations/src/integrations/Appcues/browser.js index 55ac6996b2..e2a08b5424 100644 --- a/packages/analytics-js-integrations/src/integrations/Appcues/browser.js +++ b/packages/analytics-js-integrations/src/integrations/Appcues/browser.js @@ -56,7 +56,7 @@ class Appcues { const { userId } = rudderElement.message; // iterate through traits and flatten any properties that are objects or arrays Object.keys(traits).forEach(key => { - if ( traits[key] && typeof traits[key] === 'object' ) { + if (traits[key] && typeof traits[key] === 'object') { Object.keys(traits[key]).forEach(subKey => { traits[`${key}.${subKey}`] = traits[key][subKey]; }); @@ -84,6 +84,19 @@ class Appcues { const { properties, name } = rudderElement.message; window.Appcues.page(name, properties); } + + // docs: https://docs.appcues.com/en_US/dev-installing-appcues/installation-overview-for-developers#identifying-groups-15 + group(rudderElement) { + const { groupId, traits } = rudderElement.message; + if (!isDefinedAndNotNullAndNotEmpty(groupId)) { + logger.error('group id is required'); + return; + } + window.Appcues.group( + groupId, // unique, required + traits, + ); + } } export default Appcues;