@segment/analytics-browser-actions-pendo-web-actions 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/cjs/generated-types.d.ts +1 -3
  2. package/dist/cjs/group/generated-types.d.ts +1 -1
  3. package/dist/cjs/group/index.js +24 -22
  4. package/dist/cjs/group/index.js.map +1 -1
  5. package/dist/cjs/identify/generated-types.d.ts +0 -8
  6. package/dist/cjs/identify/index.js +7 -48
  7. package/dist/cjs/identify/index.js.map +1 -1
  8. package/dist/cjs/index.js +37 -59
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/loadScript.d.ts +1 -1
  11. package/dist/cjs/loadScript.js +2 -2
  12. package/dist/cjs/loadScript.js.map +1 -1
  13. package/dist/cjs/track/index.js +4 -3
  14. package/dist/cjs/track/index.js.map +1 -1
  15. package/dist/cjs/types.d.ts +8 -16
  16. package/dist/esm/generated-types.d.ts +1 -3
  17. package/dist/esm/group/generated-types.d.ts +1 -1
  18. package/dist/esm/group/index.js +24 -22
  19. package/dist/esm/group/index.js.map +1 -1
  20. package/dist/esm/identify/generated-types.d.ts +0 -8
  21. package/dist/esm/identify/index.js +7 -48
  22. package/dist/esm/identify/index.js.map +1 -1
  23. package/dist/esm/index.js +37 -59
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/loadScript.d.ts +1 -1
  26. package/dist/esm/loadScript.js +2 -2
  27. package/dist/esm/loadScript.js.map +1 -1
  28. package/dist/esm/track/index.js +4 -3
  29. package/dist/esm/track/index.js.map +1 -1
  30. package/dist/esm/types.d.ts +8 -16
  31. package/dist/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +3 -3
  33. package/src/generated-types.ts +3 -11
  34. package/src/group/__tests__/index.test.ts +2 -1
  35. package/src/group/generated-types.ts +3 -6
  36. package/src/group/index.ts +28 -25
  37. package/src/identify/__tests__/index.test.ts +2 -9
  38. package/src/identify/generated-types.ts +1 -21
  39. package/src/identify/index.ts +10 -52
  40. package/src/index.ts +43 -64
  41. package/src/loadScript.ts +2 -2
  42. package/src/track/index.ts +4 -4
  43. package/src/types.ts +9 -11
@@ -1,7 +1,7 @@
1
1
  import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
2
2
  import type { Settings } from '../generated-types'
3
3
  import type { Payload } from './generated-types'
4
- import type { PendoSDK, identifyPayload } from '../types'
4
+ import type { PendoSDK, PendoOptions } from '../types'
5
5
 
6
6
  const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
7
7
  title: 'Send Group Event',
@@ -11,59 +11,62 @@ const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
11
11
  fields: {
12
12
  visitorId: {
13
13
  label: 'Visitor ID',
14
- description: 'Pendo Visitor ID. Defaults to Segment userId',
14
+ description: 'Pendo Visitor ID. Maps to Segment userId',
15
15
  type: 'string',
16
16
  required: true,
17
17
  default: {
18
18
  '@path': '$.userId'
19
- }
19
+ },
20
+ readOnly: true
20
21
  },
21
22
  accountId: {
22
23
  label: 'Account ID',
23
- description: 'Pendo Account ID. This overrides the Pendo Account ID setting',
24
+ description: 'Pendo Account ID',
24
25
  type: 'string',
25
26
  required: true,
26
- default: { '@path': '$.groupId' }
27
+ default: { '@path': '$.groupId' },
28
+ readOnly: true
27
29
  },
28
30
  accountData: {
29
31
  label: 'Account Metadata',
30
32
  description: 'Additional Account data to send',
31
33
  type: 'object',
32
- required: false
33
- },
34
- parentAccountId: {
35
- label: 'Parent Account ID',
36
- description:
37
- 'Pendo Parent Account ID. This overrides the Pendo Parent Account ID setting. Note: Contact Pendo to request enablement of Parent Account feature.',
38
- type: 'string',
39
- required: false
34
+ required: false,
35
+ default: { '@path': '$.traits' },
36
+ readOnly: true
40
37
  },
41
38
  parentAccountData: {
42
39
  label: 'Parent Account Metadata',
43
40
  description:
44
41
  'Additional Parent Account data to send. Note: Contact Pendo to request enablement of Parent Account feature.',
45
42
  type: 'object',
43
+ properties: {
44
+ id: {
45
+ label: 'Parent Account ID',
46
+ type: 'string',
47
+ required: true
48
+ }
49
+ },
50
+ additionalProperties: true,
51
+ default: { '@path': '$.traits.parentAccount' },
46
52
  required: false
47
53
  }
48
54
  },
49
55
  perform: (pendo, event) => {
50
- const payload: identifyPayload = {
56
+ const payload: PendoOptions = {
51
57
  visitor: {
52
58
  id: event.payload.visitorId
59
+ },
60
+ account: {
61
+ ...event.payload.accountData,
62
+ id: event.payload.accountId
53
63
  }
54
64
  }
55
- if (event.payload.accountId || event.settings.accountId) {
56
- payload.account = {
57
- id: event.payload.accountId ?? (event.settings.accountId as string),
58
- ...event.payload.accountData
59
- }
60
- }
61
- if (event.payload.parentAccountId || event.settings.parentAccountId) {
62
- payload.parentAccount = {
63
- id: (event.payload.parentAccountId as string) ?? (event.settings.parentAccountId as string),
64
- ...event.payload.parentAccountData
65
- }
65
+
66
+ if (event.payload.parentAccountData) {
67
+ payload.parentAccount = event.payload.parentAccountData
66
68
  }
69
+
67
70
  pendo.identify(payload)
68
71
  }
69
72
  }
@@ -15,9 +15,6 @@ const subscriptions: Subscription[] = [
15
15
  },
16
16
  visitorData: {
17
17
  '@path': '$.traits'
18
- },
19
- accountId: {
20
- '@path': '$.context.group_id'
21
18
  }
22
19
  }
23
20
  }
@@ -26,7 +23,6 @@ const subscriptions: Subscription[] = [
26
23
  describe('Pendo.identify', () => {
27
24
  const settings = {
28
25
  apiKey: 'abc123',
29
- setVisitorIdOnLoad: 'disabled',
30
26
  region: 'io'
31
27
  }
32
28
 
@@ -46,7 +42,8 @@ describe('Pendo.identify', () => {
46
42
  initialize: jest.fn(),
47
43
  isReady: jest.fn(),
48
44
  track: jest.fn(),
49
- identify: jest.fn()
45
+ identify: jest.fn(),
46
+ flushNow: jest.fn()
50
47
  }
51
48
  return Promise.resolve(mockPendo)
52
49
  })
@@ -59,15 +56,11 @@ describe('Pendo.identify', () => {
59
56
  userId: 'testUserId',
60
57
  traits: {
61
58
  first_name: 'Jimbo'
62
- },
63
- context: {
64
- group_id: 'company_id_1'
65
59
  }
66
60
  })
67
61
  await identifyAction.identify?.(context)
68
62
 
69
63
  expect(mockPendo.identify).toHaveBeenCalledWith({
70
- account: { id: 'company_id_1' },
71
64
  visitor: { first_name: 'Jimbo', id: 'testUserId' }
72
65
  })
73
66
  })
@@ -2,7 +2,7 @@
2
2
 
3
3
  export interface Payload {
4
4
  /**
5
- * Pendo Visitor ID. Defaults to Segment userId
5
+ * Pendo Visitor ID. Maps to Segment userId
6
6
  */
7
7
  visitorId: string
8
8
  /**
@@ -11,24 +11,4 @@ export interface Payload {
11
11
  visitorData?: {
12
12
  [k: string]: unknown
13
13
  }
14
- /**
15
- * Pendo Account ID. This overrides the Pendo Account ID setting
16
- */
17
- accountId?: string
18
- /**
19
- * Additional Account data to send
20
- */
21
- accountData?: {
22
- [k: string]: unknown
23
- }
24
- /**
25
- * Pendo Parent Account ID. This overrides the Pendo Parent Account ID setting. Note: Contact Pendo to request enablement of Parent Account feature.
26
- */
27
- parentAccountId?: string
28
- /**
29
- * Additional Parent Account data to send. Note: Contact Pendo to request enablement of Parent Account feature.
30
- */
31
- parentAccountData?: {
32
- [k: string]: unknown
33
- }
34
14
  }
@@ -1,7 +1,7 @@
1
1
  import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
2
2
  import type { Settings } from '../generated-types'
3
3
  import type { Payload } from './generated-types'
4
- import type { PendoSDK, identifyPayload } from '../types'
4
+ import type { PendoSDK, PendoOptions } from '../types'
5
5
 
6
6
  const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
7
7
  title: 'Send Identify Event',
@@ -11,12 +11,13 @@ const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
11
11
  fields: {
12
12
  visitorId: {
13
13
  label: 'Visitor ID',
14
- description: 'Pendo Visitor ID. Defaults to Segment userId',
14
+ description: 'Pendo Visitor ID. Maps to Segment userId',
15
15
  type: 'string',
16
16
  required: true,
17
17
  default: {
18
18
  '@path': '$.userId'
19
- }
19
+ },
20
+ readOnly: true
20
21
  },
21
22
  visitorData: {
22
23
  label: 'Visitor Metadata',
@@ -24,61 +25,18 @@ const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
24
25
  type: 'object',
25
26
  default: {
26
27
  '@path': '$.traits'
27
- }
28
- },
29
- accountId: {
30
- label: 'Account ID',
31
- description: 'Pendo Account ID. This overrides the Pendo Account ID setting',
32
- type: 'string',
33
- required: false,
34
- default: {
35
- '@if': {
36
- exists: { '@path': '$.context.group_id' },
37
- then: { '@path': '$.context.group_id' },
38
- else: { '@path': '$.groupId' }
39
- }
40
- }
41
- },
42
- accountData: {
43
- label: 'Account Metadata',
44
- description: 'Additional Account data to send',
45
- type: 'object',
46
- required: false
47
- },
48
- parentAccountId: {
49
- label: 'Parent Account ID',
50
- description:
51
- 'Pendo Parent Account ID. This overrides the Pendo Parent Account ID setting. Note: Contact Pendo to request enablement of Parent Account feature.',
52
- type: 'string',
53
- required: false
54
- },
55
- parentAccountData: {
56
- label: 'Parent Account Metadata',
57
- description:
58
- 'Additional Parent Account data to send. Note: Contact Pendo to request enablement of Parent Account feature.',
59
- type: 'object',
60
- required: false
28
+ },
29
+ readOnly: true
61
30
  }
62
31
  },
63
32
  perform: (pendo, event) => {
64
- const payload: identifyPayload = {
33
+ const payload: PendoOptions = {
65
34
  visitor: {
66
- id: event.payload.visitorId,
67
- ...event.payload.visitorData
68
- }
69
- }
70
- if (event.payload.accountId || event.settings.accountId) {
71
- payload.account = {
72
- id: (event.payload.accountId as string) ?? (event.settings.accountId as string),
73
- ...event.payload.accountData
74
- }
75
- }
76
- if (event.payload.parentAccountId || event.settings.parentAccountId) {
77
- payload.parentAccount = {
78
- id: (event.payload.parentAccountId as string) ?? (event.settings.parentAccountId as string),
79
- ...event.payload.parentAccountData
35
+ ...event.payload.visitorData,
36
+ id: event.payload.visitorId
80
37
  }
81
38
  }
39
+
82
40
  pendo.identify(payload)
83
41
  }
84
42
  }
package/src/index.ts CHANGED
@@ -2,7 +2,8 @@ import type { Settings } from './generated-types'
2
2
  import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
3
3
  import { browserDestination } from '@segment/browser-destination-runtime/shim'
4
4
  import { loadPendo } from './loadScript'
5
- import { InitializeData, PendoSDK } from './types'
5
+ import { PendoOptions, PendoSDK } from './types'
6
+ import { ID } from '@segment/analytics-next'
6
7
 
7
8
  import identify from './identify'
8
9
  import track from './track'
@@ -28,90 +29,68 @@ export const destination: BrowserDestinationDefinition<Settings, PendoSDK> = {
28
29
  type: 'string',
29
30
  required: true
30
31
  },
31
- accountId: {
32
- label: 'Set Pendo Account ID on Load',
33
- description:
34
- 'Segment can set the Pendo Account ID upon page load. This can be overridden via the Account ID field in the Send Identify/Group Actions',
35
- type: 'string',
36
- required: false
37
- },
38
- parentAccountId: {
39
- label: 'Set Pendo Parent Account ID on Load',
40
- description:
41
- 'Segment can set the Pendo Parent Account ID upon page load. This can be overridden via the Parent Account ID field in the Send Identify/Group Actions. Note: Contact Pendo to request enablement of Parent Account feature.',
42
- type: 'string',
43
- required: false
44
- },
45
32
  region: {
46
33
  label: 'Region',
47
34
  type: 'string',
48
- description: "The Pendo Region you'd like to send data to",
35
+ description: 'The region for your Pendo subscription.',
49
36
  required: true,
50
- default: 'io',
37
+ default: 'https://cdn.pendo.io',
51
38
  choices: [
52
- { value: 'io', label: 'io' },
53
- { value: 'eu', label: 'eu' }
39
+ { value: 'https://cdn.pendo.io', label: 'US (default)' },
40
+ { value: 'https://cdn.eu.pendo.io', label: 'EU' },
41
+ { value: 'https://us1.cdn.pendo.io', label: 'US restricted' },
42
+ { value: 'https://cdn.jpn.pendo.io', label: 'Japan' }
54
43
  ]
55
44
  },
56
- setVisitorIdOnLoad: {
57
- label: 'Set Vistor ID on Load',
45
+ cnameContentHost: {
46
+ label: 'Optional CNAME content host',
58
47
  description:
59
- 'Segment can set the Pendo Visitor ID upon page load to either the Segment userId or anonymousId. This can be overridden via the Visitor ID field in the Send Identify/Group Actions',
48
+ "If you are using Pendo's CNAME feature, this will update your Pendo install snippet with your content host.",
60
49
  type: 'string',
61
- default: 'disabled',
62
- choices: [
63
- { value: 'disabled', label: 'Do not set Visitor ID on load' },
64
- { value: 'userIdOnly', label: 'Set Visitor ID to userId on load' },
65
- { value: 'userIdOrAnonymousId', label: 'Set Visitor ID to userId or anonymousId on load' },
66
- { value: 'anonymousIdOnly', label: 'Set Visitor ID to anonymousId on load' }
67
- ],
68
- required: true
50
+ required: false
69
51
  }
70
52
  },
71
53
 
72
54
  initialize: async ({ settings, analytics }, deps) => {
73
- loadPendo(settings.apiKey, settings.region)
74
-
75
- await deps.resolveWhen(() => window.pendo != null, 100)
55
+ if (settings.cnameContentHost && !/^https?:/.exec(settings.cnameContentHost) && settings.cnameContentHost.length) {
56
+ settings.cnameContentHost = 'https://' + settings.cnameContentHost
57
+ }
76
58
 
77
- const initialData: InitializeData = {}
59
+ loadPendo(settings.apiKey, settings.region, settings.cnameContentHost)
78
60
 
79
- if (settings.setVisitorIdOnLoad) {
80
- let vistorId: string | null = null
61
+ await deps.resolveWhen(() => window.pendo != null, 100)
81
62
 
82
- switch (settings.setVisitorIdOnLoad) {
83
- case 'disabled':
84
- vistorId = null
85
- break
86
- case 'userIdOnly':
87
- vistorId = analytics.user().id() ?? null
88
- break
89
- case 'userIdOrAnonymousId':
90
- vistorId = analytics.user().id() ?? analytics.user().anonymousId() ?? null
91
- break
92
- case 'anonymousIdOnly':
93
- vistorId = analytics.user().anonymousId() ?? null
94
- break
95
- }
63
+ let visitorId: ID = null
64
+ let accountId: ID = null
96
65
 
97
- if (vistorId) {
98
- initialData.visitor = {
99
- id: vistorId
100
- }
101
- }
66
+ if (analytics.user().id()) {
67
+ visitorId = analytics.user().id()
68
+ } else if (analytics.user().anonymousId()) {
69
+ // Append Pendo anonymous visitor tag
70
+ // https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/pendo/lib/index.js#L114
71
+ visitorId = '_PENDO_T_' + analytics.user().anonymousId()
102
72
  }
103
- if (settings.accountId) {
104
- initialData.account = {
105
- id: settings.accountId
106
- }
73
+
74
+ if (analytics.group().id()) {
75
+ accountId = analytics.group().id()
107
76
  }
108
- if (settings.parentAccountId) {
109
- initialData.parentAccount = {
110
- id: settings.parentAccountId
111
- }
77
+
78
+ const options: PendoOptions = {
79
+ visitor: {
80
+ ...analytics.user().traits(),
81
+ id: visitorId
82
+ },
83
+ ...(accountId
84
+ ? {
85
+ account: {
86
+ ...analytics.group().traits(),
87
+ id: accountId
88
+ }
89
+ }
90
+ : {})
112
91
  }
113
92
 
114
- window.pendo.initialize(initialData)
93
+ window.pendo.initialize(options)
115
94
 
116
95
  return window.pendo
117
96
  },
package/src/loadScript.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable */
2
2
  // @ts-nocheck
3
- export function loadPendo(apiKey, region) {
3
+ export function loadPendo(apiKey, region, cnameContentHost) {
4
4
  ;(function (p, e, n, d, o) {
5
5
  var v, w, x, y, z
6
6
  o = p[d] = p[d] || {}
@@ -16,7 +16,7 @@ export function loadPendo(apiKey, region) {
16
16
  })(v[w])
17
17
  y = e.createElement(n)
18
18
  y.async = !0
19
- y.src = `https://cdn.pendo.${region}/agent/static/` + apiKey + '/pendo.js'
19
+ y.src = `${cnameContentHost ?? region}/agent/static/${apiKey}/pendo.js`
20
20
  z = e.getElementsByTagName(n)[0]
21
21
  z.parentNode.insertBefore(y, z)
22
22
  })(window, document, 'script', 'pendo')
@@ -3,7 +3,6 @@ import type { Settings } from '../generated-types'
3
3
  import type { Payload } from './generated-types'
4
4
  import type { PendoSDK } from '../types'
5
5
 
6
- // Change from unknown to the partner SDK types
7
6
  const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
8
7
  title: 'Send Track Event',
9
8
  description: 'Send Segment track() events to Pendo',
@@ -17,7 +16,8 @@ const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
17
16
  required: true,
18
17
  default: {
19
18
  '@path': '$.event'
20
- }
19
+ },
20
+ readOnly: true
21
21
  },
22
22
  metadata: {
23
23
  label: 'Metadata',
@@ -25,12 +25,12 @@ const action: BrowserActionDefinition<Settings, PendoSDK, Payload> = {
25
25
  type: 'object',
26
26
  default: {
27
27
  '@path': '$.properties'
28
- }
28
+ },
29
+ readOnly: true
29
30
  }
30
31
  },
31
32
  perform: (pendo, { payload }) => {
32
33
  pendo.track(payload.event, payload.metadata)
33
- pendo.flushNow(true)
34
34
  }
35
35
  }
36
36
 
package/src/types.ts CHANGED
@@ -1,27 +1,25 @@
1
+ import { ID } from '@segment/analytics-next'
2
+
1
3
  export type Visitor = {
2
- id?: string | null | undefined
4
+ id: ID
5
+ [propName: string]: unknown
3
6
  }
4
7
 
5
8
  export type Account = {
6
- id?: string | null | undefined
9
+ id: ID
10
+ [propName: string]: unknown
7
11
  }
8
12
 
9
- export type InitializeData = {
13
+ export type PendoOptions = {
10
14
  visitor?: Visitor
11
15
  account?: Account
12
16
  parentAccount?: Account
13
17
  }
14
18
 
15
- export type identifyPayload = {
16
- visitor: { [key: string]: string }
17
- account?: { [key: string]: string }
18
- parentAccount?: { [key: string]: string }
19
- }
20
-
21
19
  export type PendoSDK = {
22
- initialize: ({ visitor, account }: InitializeData) => void
20
+ initialize: ({ visitor, account }: PendoOptions) => void
23
21
  track: (eventName: string, metadata?: { [key: string]: unknown }) => void
24
- identify: (data: identifyPayload) => void
22
+ identify: (data: PendoOptions) => void
25
23
  flushNow: (force: boolean) => void
26
24
  isReady: () => boolean
27
25
  }