@segment/analytics-browser-actions-intercom 1.0.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.
- package/package.json +21 -0
- package/src/__tests__/index.test.ts +55 -0
- package/src/__tests__/utils.test.ts +90 -0
- package/src/api.ts +11 -0
- package/src/generated-types.ts +20 -0
- package/src/identifyCompany/__tests__/index.test.ts +308 -0
- package/src/identifyCompany/generated-types.ts +51 -0
- package/src/identifyCompany/index.ts +82 -0
- package/src/identifyUser/__tests__/index.test.ts +460 -0
- package/src/identifyUser/generated-types.ts +136 -0
- package/src/identifyUser/index.ts +237 -0
- package/src/index.ts +109 -0
- package/src/init-script.ts +49 -0
- package/src/sharedCompanyProperties.ts +59 -0
- package/src/trackEvent/__tests__/index.test.ts +179 -0
- package/src/trackEvent/generated-types.ts +22 -0
- package/src/trackEvent/index.ts +92 -0
- package/src/utils.ts +43 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
|
|
2
|
+
import { Intercom } from '../api'
|
|
3
|
+
import type { Settings } from '../generated-types'
|
|
4
|
+
import { filterCustomTraits, isEmpty } from '../utils'
|
|
5
|
+
import type { Payload } from './generated-types'
|
|
6
|
+
|
|
7
|
+
const action: BrowserActionDefinition<Settings, Intercom, Payload> = {
|
|
8
|
+
title: 'Track Event',
|
|
9
|
+
description: 'Submit an event to Intercom.',
|
|
10
|
+
defaultSubscription: 'type = "track"',
|
|
11
|
+
platform: 'web',
|
|
12
|
+
fields: {
|
|
13
|
+
event_name: {
|
|
14
|
+
description: 'The name of the event.',
|
|
15
|
+
label: 'Event Name',
|
|
16
|
+
type: 'string',
|
|
17
|
+
required: true,
|
|
18
|
+
default: {
|
|
19
|
+
'@path': '$.event'
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
revenue: {
|
|
23
|
+
description:
|
|
24
|
+
'The amount associated with a purchase. Segment will multiply by 100 as Intercom requires the amount in cents.',
|
|
25
|
+
label: 'Revenue',
|
|
26
|
+
type: 'number',
|
|
27
|
+
required: false,
|
|
28
|
+
default: {
|
|
29
|
+
'@path': '$.properties.revenue'
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
currency: {
|
|
33
|
+
description:
|
|
34
|
+
'The currency of the purchase amount. Segment will default to USD if revenue is provided without a currency.',
|
|
35
|
+
label: 'Currency',
|
|
36
|
+
type: 'string',
|
|
37
|
+
required: false,
|
|
38
|
+
default: {
|
|
39
|
+
'@path': '$.properties.currency'
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
event_metadata: {
|
|
43
|
+
description: 'Optional metadata describing the event.',
|
|
44
|
+
label: 'Event Metadata',
|
|
45
|
+
type: 'object',
|
|
46
|
+
required: false,
|
|
47
|
+
default: {
|
|
48
|
+
'@path': '$.properties'
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
perform: (Intercom, event) => {
|
|
53
|
+
//remove event_name & event_metadata from the payload (they will be handled separately)
|
|
54
|
+
const { event_name, event_metadata, ...rest } = event.payload
|
|
55
|
+
const payload = { ...rest }
|
|
56
|
+
const richLinkProperties = Intercom.richLinkProperties ? Intercom.richLinkProperties : []
|
|
57
|
+
|
|
58
|
+
// create a list of the richLinkObjects that will be passed to Intercom
|
|
59
|
+
const richLinkObjects: { [k: string]: unknown } = {}
|
|
60
|
+
if (event_metadata && richLinkProperties.length != 0) {
|
|
61
|
+
Object.entries(event_metadata).forEach(([key, value]) => {
|
|
62
|
+
if (richLinkProperties.includes(key)) {
|
|
63
|
+
richLinkObjects[key] = value
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// filter out reserved fields, drop custom objects & arrays
|
|
69
|
+
const filteredMetadata = filterCustomTraits(event_metadata)
|
|
70
|
+
|
|
71
|
+
// create price object
|
|
72
|
+
let price = {}
|
|
73
|
+
if (payload.revenue) {
|
|
74
|
+
price = {
|
|
75
|
+
amount: payload.revenue * 100,
|
|
76
|
+
currency: payload.currency ?? 'USD'
|
|
77
|
+
}
|
|
78
|
+
delete filteredMetadata.revenue
|
|
79
|
+
delete filteredMetadata.currency
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//merge richLinkObjects into the final payload
|
|
83
|
+
//API call
|
|
84
|
+
Intercom('trackEvent', event_name, {
|
|
85
|
+
...filteredMetadata,
|
|
86
|
+
...richLinkObjects,
|
|
87
|
+
...(!isEmpty(price) && { price })
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default action
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { isArray, isObject } from '@segment/actions-core'
|
|
2
|
+
import { isNonEmpty } from '@segment/actions-shared'
|
|
3
|
+
import dayjs from 'dayjs'
|
|
4
|
+
|
|
5
|
+
export function convertDateToUnix(created_at: string | number): number {
|
|
6
|
+
if (typeof created_at === 'number') {
|
|
7
|
+
let unixDate = dayjs.unix(created_at).unix()
|
|
8
|
+
if (unixDate.toString().length == 13) {
|
|
9
|
+
unixDate = Math.floor(unixDate / 1000)
|
|
10
|
+
}
|
|
11
|
+
return unixDate
|
|
12
|
+
}
|
|
13
|
+
return dayjs(created_at).unix()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function filterCustomTraits(traits: { [k: string]: unknown } | undefined) {
|
|
17
|
+
const filteredCustomTraits: { [k: string]: unknown } = {}
|
|
18
|
+
if (traits) {
|
|
19
|
+
for (const [key, value] of Object.entries(traits)) {
|
|
20
|
+
if (!isArray(value) && !isObject(value)) {
|
|
21
|
+
filteredCustomTraits[key] = value
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return filteredCustomTraits
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getWidgetOptions(hide_default_launcher: boolean | undefined, activator: string | undefined) {
|
|
29
|
+
const widgetOptions: { [key: string]: unknown } = {}
|
|
30
|
+
if (hide_default_launcher !== undefined) {
|
|
31
|
+
widgetOptions.hide_default_launcher = hide_default_launcher
|
|
32
|
+
}
|
|
33
|
+
if (activator !== '#IntercomDefaultWidget') {
|
|
34
|
+
widgetOptions.widget = {
|
|
35
|
+
activator: activator
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return widgetOptions
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isEmpty(o: object | undefined) {
|
|
42
|
+
return !isNonEmpty(o)
|
|
43
|
+
}
|