From 2879de3fa3d77bc77ece7ddb37cf31a75a1d98b5 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 23 Jan 2023 18:28:28 +0100 Subject: [PATCH] Update Ripe web destination (#968) * Update Ripe web destination - Add new endpoint setting for testing purposes - Remove alias call - Update misleading anonymousId descriptions * Update erroneous default paths * Set anonymousId in identify call --- .../ripe/alias/__tests__/index.test.ts | 100 ------------------ .../ripe/alias/generated-types.ts | 16 --- .../src/destinations/ripe/alias/index.ts | 46 -------- .../src/destinations/ripe/generated-types.ts | 4 + .../ripe/group/generated-types.ts | 2 +- .../src/destinations/ripe/group/index.ts | 2 +- .../ripe/identify/__tests__/index.test.ts | 6 ++ .../ripe/identify/generated-types.ts | 2 +- .../src/destinations/ripe/identify/index.ts | 7 +- .../src/destinations/ripe/index.ts | 19 ++-- .../src/destinations/ripe/init-script.ts | 2 +- .../destinations/ripe/page/generated-types.ts | 2 +- .../src/destinations/ripe/page/index.ts | 20 +++- .../ripe/track/generated-types.ts | 2 +- .../src/destinations/ripe/track/index.ts | 4 +- .../src/destinations/ripe/types.ts | 3 +- 16 files changed, 48 insertions(+), 189 deletions(-) delete mode 100644 packages/browser-destinations/src/destinations/ripe/alias/__tests__/index.test.ts delete mode 100644 packages/browser-destinations/src/destinations/ripe/alias/generated-types.ts delete mode 100644 packages/browser-destinations/src/destinations/ripe/alias/index.ts diff --git a/packages/browser-destinations/src/destinations/ripe/alias/__tests__/index.test.ts b/packages/browser-destinations/src/destinations/ripe/alias/__tests__/index.test.ts deleted file mode 100644 index 5c72fcf040..0000000000 --- a/packages/browser-destinations/src/destinations/ripe/alias/__tests__/index.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -import type { Subscription } from '../../../../lib/browser-destinations' -import { Analytics, Context } from '@segment/analytics-next' -import RipeDestination, { destination } from '../../index' -import { RipeSDK } from '../../types' - -import { loadScript } from '../../../../runtime/load-script' - -jest.mock('../../../../runtime/load-script') -beforeEach(async () => { - ;(loadScript as jest.Mock).mockResolvedValue(true) -}) - -const subscriptions: Subscription[] = [ - { - partnerAction: 'alias', - name: 'Alias user', - enabled: true, - subscribe: 'type = "alias"', - mapping: { - userId: { - '@path': '$.userId' - }, - anonymousId: { - '@path': '$.anonymousId' - } - } - } -] - -describe('Ripe.alias', () => { - test('it maps userId and passes it into RipeSDK.alias', async () => { - window.Ripe = { - init: jest.fn().mockResolvedValueOnce('123'), - alias: jest.fn().mockResolvedValueOnce(undefined), - setIds: jest.fn().mockResolvedValueOnce(undefined) - } as unknown as RipeSDK - - const [event] = await RipeDestination({ - subscriptions, - apiKey: '123' - }) - - const ajs = new Analytics({ writeKey: '123' }) - await event.load(Context.system(), ajs) - jest.spyOn(destination.actions.alias, 'perform') - - await event.alias?.( - new Context({ - type: 'alias', - userId: 'newId' - }) - ) - - expect(destination.actions.alias.perform).toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ - payload: { - userId: 'newId' - } - }) - ) - - expect(window.Ripe.alias).toHaveBeenCalledWith('newId') - }) - - test('it maps anonymousId if userId is not set and passes it into RipeSDK.alias', async () => { - window.Ripe = { - init: jest.fn().mockResolvedValueOnce('123'), - alias: jest.fn().mockResolvedValueOnce(undefined), - setIds: jest.fn().mockResolvedValueOnce(undefined) - } as unknown as RipeSDK - - const [event] = await RipeDestination({ - subscriptions, - apiKey: '123' - }) - - const ajs = new Analytics({ writeKey: '123' }) - await event.load(Context.system(), ajs) - jest.spyOn(destination.actions.alias, 'perform') - - await event.alias?.( - new Context({ - type: 'alias', - anonymousId: 'anonymousId' - }) - ) - - expect(destination.actions.alias.perform).toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ - payload: { - anonymousId: 'anonymousId' - } - }) - ) - - expect(window.Ripe.alias).toHaveBeenCalledWith('anonymousId') - }) -}) diff --git a/packages/browser-destinations/src/destinations/ripe/alias/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/alias/generated-types.ts deleted file mode 100644 index 489a8e35cf..0000000000 --- a/packages/browser-destinations/src/destinations/ripe/alias/generated-types.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Generated file. DO NOT MODIFY IT BY HAND. - -export interface Payload { - /** - * The new user ID, if user ID is not set - */ - anonymousId: string - /** - * The ID associated with the user - */ - userId?: string - /** - * The ID associated groupId - */ - groupId?: string -} diff --git a/packages/browser-destinations/src/destinations/ripe/alias/index.ts b/packages/browser-destinations/src/destinations/ripe/alias/index.ts deleted file mode 100644 index 02c39aea4b..0000000000 --- a/packages/browser-destinations/src/destinations/ripe/alias/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import type { BrowserActionDefinition } from '../../../lib/browser-destinations' -import type { Settings } from '../generated-types' -import type { Payload } from './generated-types' -import { RipeSDK } from '../types' - -const action: BrowserActionDefinition = { - title: 'Alias', - description: 'Alias a user to new user ID in Ripe', - defaultSubscription: 'type = "alias"', - platform: 'web', - fields: { - anonymousId: { - type: 'string', - required: true, - description: 'The new user ID, if user ID is not set', - label: 'Anonymous ID', - default: { '@path': '$.anonymousId' } - }, - userId: { - type: 'string', - required: false, - description: 'The ID associated with the user', - label: 'User ID', - default: { '@path': '$.userId' } - }, - groupId: { - type: 'string', - required: false, - description: 'The ID associated groupId', - label: 'Group ID', - default: { '@path': '$.groupId' } - } - }, - perform: async (ripe, { payload }) => { - await ripe.setIds(payload.anonymousId, payload.userId, payload.groupId) - if (payload.userId) { - return ripe.alias(payload.userId) - } - - if (payload.anonymousId) { - return ripe.alias(payload.anonymousId) - } - } -} - -export default action diff --git a/packages/browser-destinations/src/destinations/ripe/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/generated-types.ts index d23e286a4d..e29ccf1367 100644 --- a/packages/browser-destinations/src/destinations/ripe/generated-types.ts +++ b/packages/browser-destinations/src/destinations/ripe/generated-types.ts @@ -9,4 +9,8 @@ export interface Settings { * The Ripe API key found in the Ripe App */ apiKey: string + /** + * The Ripe API endpoint (do not change this unless you know what you're doing) + */ + endpoint?: string } diff --git a/packages/browser-destinations/src/destinations/ripe/group/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/group/generated-types.ts index 1dccedcdba..2d62b12b15 100644 --- a/packages/browser-destinations/src/destinations/ripe/group/generated-types.ts +++ b/packages/browser-destinations/src/destinations/ripe/group/generated-types.ts @@ -2,7 +2,7 @@ export interface Payload { /** - * The new user ID, if user ID is not set + * The anonymous id */ anonymousId: string /** diff --git a/packages/browser-destinations/src/destinations/ripe/group/index.ts b/packages/browser-destinations/src/destinations/ripe/group/index.ts index 9f7ec0294c..3f814039e9 100644 --- a/packages/browser-destinations/src/destinations/ripe/group/index.ts +++ b/packages/browser-destinations/src/destinations/ripe/group/index.ts @@ -12,7 +12,7 @@ const action: BrowserActionDefinition = { anonymousId: { type: 'string', required: true, - description: 'The new user ID, if user ID is not set', + description: 'The anonymous id', label: 'Anonymous ID', default: { '@path': '$.anonymousId' } }, diff --git a/packages/browser-destinations/src/destinations/ripe/identify/__tests__/index.test.ts b/packages/browser-destinations/src/destinations/ripe/identify/__tests__/index.test.ts index 29e58e24f1..ba7225f353 100644 --- a/packages/browser-destinations/src/destinations/ripe/identify/__tests__/index.test.ts +++ b/packages/browser-destinations/src/destinations/ripe/identify/__tests__/index.test.ts @@ -17,6 +17,9 @@ const subscriptions: Subscription[] = [ enabled: true, subscribe: 'type = "identify"', mapping: { + anonymousId: { + '@path': '$.anonymousId' + }, userId: { '@path': '$.userId' }, @@ -47,6 +50,7 @@ describe('Ripe.identify', () => { await event.identify?.( new Context({ type: 'identify', + anonymousId: 'anonymousId', userId: 'userId', traits: { name: 'Simon' @@ -58,6 +62,7 @@ describe('Ripe.identify', () => { expect.anything(), expect.objectContaining({ payload: { + anonymousId: 'anonymousId', userId: 'userId', traits: { name: 'Simon' @@ -67,6 +72,7 @@ describe('Ripe.identify', () => { ) expect(window.Ripe.identify).toHaveBeenCalledWith( + expect.stringMatching('anonymousId'), expect.stringMatching('userId'), expect.objectContaining({ name: 'Simon' }) ) diff --git a/packages/browser-destinations/src/destinations/ripe/identify/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/identify/generated-types.ts index 28b9488f88..b9ef09efdd 100644 --- a/packages/browser-destinations/src/destinations/ripe/identify/generated-types.ts +++ b/packages/browser-destinations/src/destinations/ripe/identify/generated-types.ts @@ -2,7 +2,7 @@ export interface Payload { /** - * The new user ID, if user ID is not set + * The anonymous id */ anonymousId: string /** diff --git a/packages/browser-destinations/src/destinations/ripe/identify/index.ts b/packages/browser-destinations/src/destinations/ripe/identify/index.ts index 75d7666692..408f023566 100644 --- a/packages/browser-destinations/src/destinations/ripe/identify/index.ts +++ b/packages/browser-destinations/src/destinations/ripe/identify/index.ts @@ -12,7 +12,7 @@ const action: BrowserActionDefinition = { anonymousId: { type: 'string', required: true, - description: 'The new user ID, if user ID is not set', + description: 'The anonymous id', label: 'Anonymous ID', default: { '@path': '$.anonymousId' } }, @@ -28,7 +28,7 @@ const action: BrowserActionDefinition = { required: false, description: 'The ID associated groupId', label: 'Group ID', - default: { '@path': '$.groupId' } + default: { '@path': '$.context.groupId' } }, traits: { type: 'object', @@ -39,8 +39,9 @@ const action: BrowserActionDefinition = { } }, perform: async (ripe, { payload }) => { + console.log(JSON.stringify(payload, null, 2)) await ripe.setIds(payload.anonymousId, payload.userId, payload.groupId) - return ripe.identify(payload.userId, payload.traits) + return ripe.identify(payload.anonymousId, payload.userId, payload.traits) } } diff --git a/packages/browser-destinations/src/destinations/ripe/index.ts b/packages/browser-destinations/src/destinations/ripe/index.ts index 921054c4da..a6d1a48c67 100644 --- a/packages/browser-destinations/src/destinations/ripe/index.ts +++ b/packages/browser-destinations/src/destinations/ripe/index.ts @@ -12,8 +12,6 @@ import { initScript } from './init-script' import page from './page' -import alias from './alias' - const defaultVersion = 'latest' declare global { @@ -46,6 +44,13 @@ export const destination: BrowserDestinationDefinition = { label: 'API Key', type: 'string', required: true + }, + endpoint: { + label: 'API Endpoint', + description: `The Ripe API endpoint (do not change this unless you know what you're doing)`, + type: 'string', + format: 'uri', + default: 'https://storage.getripe.com' } }, @@ -54,9 +59,10 @@ export const destination: BrowserDestinationDefinition = { const { sdkVersion, apiKey } = settings const version = sdkVersion ?? defaultVersion + const endpoint = settings.endpoint || 'https://storage.getripe.com' await deps - .loadScript(`https://storage.googleapis.com/sdk.getripe.com/sdk/${version}/sdk.umd.js`) + .loadScript(`${endpoint}/sdk/${version}/sdk.umd.js`) .catch((err) => console.error('Unable to load Ripe SDK script', err)) await deps.resolveWhen(() => Object.prototype.hasOwnProperty.call(window, 'Ripe'), 100) @@ -66,7 +72,6 @@ export const destination: BrowserDestinationDefinition = { }, actions: { - alias, group, identify, page, @@ -74,12 +79,6 @@ export const destination: BrowserDestinationDefinition = { }, presets: [ - { - name: 'Alias user', - subscribe: 'type = "alias"', - partnerAction: 'alias', - mapping: defaultValues(alias.fields) - }, { name: 'Group user', subscribe: 'type = "group"', diff --git a/packages/browser-destinations/src/destinations/ripe/init-script.ts b/packages/browser-destinations/src/destinations/ripe/init-script.ts index 6da5a42e46..9d075d896a 100644 --- a/packages/browser-destinations/src/destinations/ripe/init-script.ts +++ b/packages/browser-destinations/src/destinations/ripe/init-script.ts @@ -6,7 +6,7 @@ export function initScript() { } window.Ripe = [] - ;['alias', 'group', 'identify', 'init', 'page', 'setIds', 'track'].forEach(function (method) { + ;['group', 'identify', 'init', 'page', 'setIds', 'track'].forEach(function (method) { window.Ripe[method] = function () { let args = Array.from(arguments) args.unshift(method) diff --git a/packages/browser-destinations/src/destinations/ripe/page/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/page/generated-types.ts index cec9e72c62..5fbda49ba9 100644 --- a/packages/browser-destinations/src/destinations/ripe/page/generated-types.ts +++ b/packages/browser-destinations/src/destinations/ripe/page/generated-types.ts @@ -2,7 +2,7 @@ export interface Payload { /** - * The new user ID, if user ID is not set + * The anonymous id */ anonymousId: string /** diff --git a/packages/browser-destinations/src/destinations/ripe/page/index.ts b/packages/browser-destinations/src/destinations/ripe/page/index.ts index 97640d1d9c..a13cb38359 100644 --- a/packages/browser-destinations/src/destinations/ripe/page/index.ts +++ b/packages/browser-destinations/src/destinations/ripe/page/index.ts @@ -12,7 +12,7 @@ const action: BrowserActionDefinition = { anonymousId: { type: 'string', required: true, - description: 'The new user ID, if user ID is not set', + description: 'The anonymous id', label: 'Anonymous ID', default: { '@path': '$.anonymousId' } }, @@ -28,21 +28,33 @@ const action: BrowserActionDefinition = { required: false, description: 'The ID associated groupId', label: 'Group ID', - default: { '@path': '$.groupId' } + default: { '@path': '$.context.groupId' } }, category: { type: 'string', required: false, description: 'The category of the page', label: 'Category', - default: { '@path': '$.category' } + default: { + '@if': { + exists: { '@path': '$.category' }, + then: { '@path': '$.category' }, + else: { '@path': '$.context.category' } + } + } }, name: { type: 'string', required: false, description: 'The name of the page', label: 'Name', - default: { '@path': '$.name' } + default: { + '@if': { + exists: { '@path': '$.name' }, + then: { '@path': '$.name' }, + else: { '@path': '$.context.name' } + } + } }, properties: { type: 'object', diff --git a/packages/browser-destinations/src/destinations/ripe/track/generated-types.ts b/packages/browser-destinations/src/destinations/ripe/track/generated-types.ts index 4d87cccad5..9f035e260f 100644 --- a/packages/browser-destinations/src/destinations/ripe/track/generated-types.ts +++ b/packages/browser-destinations/src/destinations/ripe/track/generated-types.ts @@ -2,7 +2,7 @@ export interface Payload { /** - * The new user ID, if user ID is not set + * The anonymous id */ anonymousId: string /** diff --git a/packages/browser-destinations/src/destinations/ripe/track/index.ts b/packages/browser-destinations/src/destinations/ripe/track/index.ts index 61e9cf0599..5543963c24 100644 --- a/packages/browser-destinations/src/destinations/ripe/track/index.ts +++ b/packages/browser-destinations/src/destinations/ripe/track/index.ts @@ -12,7 +12,7 @@ const action: BrowserActionDefinition = { anonymousId: { type: 'string', required: true, - description: 'The new user ID, if user ID is not set', + description: 'The anonymous id', label: 'Anonymous ID', default: { '@path': '$.anonymousId' } }, @@ -28,7 +28,7 @@ const action: BrowserActionDefinition = { required: false, description: 'The ID associated groupId', label: 'Group ID', - default: { '@path': '$.groupId' } + default: { '@path': '$.context.groupId' } }, event: { type: 'string', diff --git a/packages/browser-destinations/src/destinations/ripe/types.ts b/packages/browser-destinations/src/destinations/ripe/types.ts index 93818aca42..9ba0298646 100644 --- a/packages/browser-destinations/src/destinations/ripe/types.ts +++ b/packages/browser-destinations/src/destinations/ripe/types.ts @@ -1,7 +1,6 @@ export interface RipeSDK { - alias: (userId: string) => Promise group: (groupId: string, traits?: Record) => Promise - identify: (userId?: string | undefined | null, traits?: Record) => Promise + identify: (anonymousId: string, userId?: string | undefined | null, traits?: Record) => Promise init: (apiKey: string) => Promise page: (category?: string, name?: string, properties?: Record) => Promise setIds: (anonymousId: string, userId?: string, groupId?: string) => Promise