@segment/analytics-browser-actions-braze 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 +40 -0
- package/src/__tests__/__snapshots__/initialization.test.ts.snap +24 -0
- package/src/__tests__/__snapshots__/integration.test.ts.snap +96 -0
- package/src/__tests__/__snapshots__/trackPurchase.test.ts.snap +97 -0
- package/src/__tests__/debounce.test.ts +133 -0
- package/src/__tests__/initialization.test.ts +139 -0
- package/src/__tests__/integration.test.ts +156 -0
- package/src/__tests__/trackEvent.test.ts +65 -0
- package/src/__tests__/trackPurchase.test.ts +65 -0
- package/src/__tests__/updateUserProfile.test.ts +145 -0
- package/src/braze-types.ts +9 -0
- package/src/debounce/generated-types.ts +3 -0
- package/src/debounce/index.ts +67 -0
- package/src/generated-types.ts +104 -0
- package/src/index.ts +358 -0
- package/src/trackEvent/generated-types.ts +14 -0
- package/src/trackEvent/index.ts +40 -0
- package/src/trackPurchase/generated-types.ts +31 -0
- package/src/trackPurchase/index.ts +83 -0
- package/src/updateUserProfile/generated-types.ts +70 -0
- package/src/updateUserProfile/index.ts +227 -0
- package/tsconfig.json +9 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import type { Settings } from './generated-types'
|
|
2
|
+
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
|
|
3
|
+
import { browserDestination } from '@segment/browser-destination-runtime/shim'
|
|
4
|
+
import type braze from '@braze/web-sdk'
|
|
5
|
+
import type appboy from '@braze/web-sdk-v3'
|
|
6
|
+
import trackEvent from './trackEvent'
|
|
7
|
+
import updateUserProfile from './updateUserProfile'
|
|
8
|
+
import trackPurchase from './trackPurchase'
|
|
9
|
+
import debounce, { resetUserCache } from './debounce'
|
|
10
|
+
import { defaultValues, DestinationDefinition } from '@segment/actions-core'
|
|
11
|
+
import { BrazeDestinationClient } from './braze-types'
|
|
12
|
+
|
|
13
|
+
declare global {
|
|
14
|
+
interface Window {
|
|
15
|
+
braze: typeof braze
|
|
16
|
+
appboy: typeof appboy
|
|
17
|
+
BRAZE_BASE_URL?: string
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const defaultVersion = '4.6'
|
|
22
|
+
|
|
23
|
+
const presets: DestinationDefinition['presets'] = [
|
|
24
|
+
{
|
|
25
|
+
name: 'Identify Calls',
|
|
26
|
+
subscribe: 'type = "identify" or type = "group"',
|
|
27
|
+
partnerAction: 'updateUserProfile',
|
|
28
|
+
mapping: defaultValues(updateUserProfile.fields)
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Order Completed calls',
|
|
32
|
+
subscribe: 'type = "track" and event = "Order Completed"',
|
|
33
|
+
partnerAction: 'trackPurchase',
|
|
34
|
+
mapping: defaultValues(trackPurchase.fields)
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'Track Calls',
|
|
38
|
+
subscribe: 'type = "track" and event != "Order Completed"',
|
|
39
|
+
partnerAction: 'trackEvent',
|
|
40
|
+
mapping: {
|
|
41
|
+
...defaultValues(trackEvent.fields),
|
|
42
|
+
eventName: {
|
|
43
|
+
'@path': '$.event'
|
|
44
|
+
},
|
|
45
|
+
eventProperties: {
|
|
46
|
+
'@path': '$.properties'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
export const destination: BrowserDestinationDefinition<Settings, BrazeDestinationClient> = {
|
|
53
|
+
name: 'Braze Web Mode (Actions)',
|
|
54
|
+
slug: 'actions-braze-web',
|
|
55
|
+
mode: 'device',
|
|
56
|
+
settings: {
|
|
57
|
+
sdkVersion: {
|
|
58
|
+
description: 'The version of the Braze SDK to use',
|
|
59
|
+
label: 'SDK Version',
|
|
60
|
+
type: 'string',
|
|
61
|
+
choices: [
|
|
62
|
+
{
|
|
63
|
+
value: '3.1',
|
|
64
|
+
label: '3.1'
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
value: '3.3',
|
|
68
|
+
label: '3.3'
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
value: '3.5',
|
|
72
|
+
label: '3.5'
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
value: '4.1',
|
|
76
|
+
label: '4.1'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
value: '4.6',
|
|
80
|
+
label: '4.6'
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
default: defaultVersion,
|
|
84
|
+
required: true
|
|
85
|
+
},
|
|
86
|
+
api_key: {
|
|
87
|
+
description: 'Found in the Braze Dashboard under Manage Settings → Apps → Web',
|
|
88
|
+
label: 'API Key',
|
|
89
|
+
type: 'string', // SDK API keys are not secret
|
|
90
|
+
required: true
|
|
91
|
+
},
|
|
92
|
+
endpoint: {
|
|
93
|
+
label: 'SDK Endpoint',
|
|
94
|
+
description:
|
|
95
|
+
'Your Braze SDK endpoint. [See more details](https://www.braze.com/docs/user_guide/administrative/access_braze/sdk_endpoints/)',
|
|
96
|
+
type: 'string',
|
|
97
|
+
format: 'uri',
|
|
98
|
+
choices: [
|
|
99
|
+
{ label: 'US-01 (https://dashboard-01.braze.com)', value: 'sdk.iad-01.braze.com' },
|
|
100
|
+
{ label: 'US-02 (https://dashboard-02.braze.com)', value: 'sdk.iad-02.braze.com' },
|
|
101
|
+
{ label: 'US-03 (https://dashboard-03.braze.com)', value: 'sdk.iad-03.braze.com' },
|
|
102
|
+
{ label: 'US-04 (https://dashboard-04.braze.com)', value: 'sdk.iad-04.braze.com' },
|
|
103
|
+
{ label: 'US-05 (https://dashboard-05.braze.com)', value: 'sdk.iad-05.braze.com' },
|
|
104
|
+
{ label: 'US-06 (https://dashboard-06.braze.com)', value: 'sdk.iad-06.braze.com' },
|
|
105
|
+
{ label: 'US-08 (https://dashboard-08.braze.com)', value: 'sdk.iad-08.braze.com' },
|
|
106
|
+
{ label: 'EU-01 (https://dashboard-01.braze.eu)', value: 'sdk.fra-01.braze.eu' },
|
|
107
|
+
{ label: 'EU-02 (https://dashboard-02.braze.eu)', value: 'sdk.fra-02.braze.eu' }
|
|
108
|
+
],
|
|
109
|
+
default: 'sdk.iad-01.braze.com',
|
|
110
|
+
required: true
|
|
111
|
+
},
|
|
112
|
+
allowCrawlerActivity: {
|
|
113
|
+
description:
|
|
114
|
+
'Allow Braze to log activity from crawlers. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)',
|
|
115
|
+
label: 'Allow Crawler Activity',
|
|
116
|
+
default: false,
|
|
117
|
+
type: 'boolean',
|
|
118
|
+
required: false
|
|
119
|
+
},
|
|
120
|
+
allowUserSuppliedJavascript: {
|
|
121
|
+
description:
|
|
122
|
+
'To indicate that you trust the Braze dashboard users to write non-malicious Javascript click actions, set this property to true. If enableHtmlInAppMessages is true, this option will also be set to true. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)',
|
|
123
|
+
label: 'Allow User Supplied Javascript',
|
|
124
|
+
default: false,
|
|
125
|
+
type: 'boolean',
|
|
126
|
+
required: false
|
|
127
|
+
},
|
|
128
|
+
deferUntilIdentified: {
|
|
129
|
+
description:
|
|
130
|
+
'If enabled, this setting delays initialization of the Braze SDK until the user has been identified. When enabled, events for anonymous users will no longer be sent to Braze.',
|
|
131
|
+
label: 'Only Track Known Users',
|
|
132
|
+
default: false,
|
|
133
|
+
type: 'boolean',
|
|
134
|
+
required: false
|
|
135
|
+
},
|
|
136
|
+
appVersion: {
|
|
137
|
+
description:
|
|
138
|
+
'Version to which user events sent to Braze will be associated with. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)',
|
|
139
|
+
label: 'App Version',
|
|
140
|
+
type: 'string',
|
|
141
|
+
required: false
|
|
142
|
+
},
|
|
143
|
+
contentSecurityNonce: {
|
|
144
|
+
description:
|
|
145
|
+
'Allows Braze to add the nonce to any <script> and <style> elements created by the SDK. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)',
|
|
146
|
+
label: 'Content Security nonce',
|
|
147
|
+
type: 'string',
|
|
148
|
+
required: false
|
|
149
|
+
},
|
|
150
|
+
devicePropertyAllowlist: {
|
|
151
|
+
label: 'Device Property Allow List',
|
|
152
|
+
description:
|
|
153
|
+
'By default, the Braze SDK automatically detects and collects all device properties in DeviceProperties. To override this behavior, provide an array of DeviceProperties. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)',
|
|
154
|
+
type: 'string',
|
|
155
|
+
required: false,
|
|
156
|
+
multiple: true
|
|
157
|
+
},
|
|
158
|
+
disablePushTokenMaintenance: {
|
|
159
|
+
label: 'Disable Push Token Maintenance',
|
|
160
|
+
type: 'boolean',
|
|
161
|
+
default: false,
|
|
162
|
+
required: false,
|
|
163
|
+
description:
|
|
164
|
+
'By default, users who have already granted web push permission will sync their push token with the Braze backend automatically on new session to ensure deliverability. To disable this behavior, set this option to true'
|
|
165
|
+
},
|
|
166
|
+
doNotLoadFontAwesome: {
|
|
167
|
+
label: 'Do Not Load Font Awesome',
|
|
168
|
+
type: 'boolean',
|
|
169
|
+
default: false,
|
|
170
|
+
description:
|
|
171
|
+
'Braze automatically loads FontAwesome 4.7.0 from the FontAwesome CDN. To disable this behavior set this option to true.'
|
|
172
|
+
},
|
|
173
|
+
enableLogging: {
|
|
174
|
+
label: 'Enable Logging',
|
|
175
|
+
required: false,
|
|
176
|
+
default: false,
|
|
177
|
+
type: 'boolean',
|
|
178
|
+
description: 'Set to true to enable logging by default'
|
|
179
|
+
},
|
|
180
|
+
enableSdkAuthentication: {
|
|
181
|
+
label: 'Enable SDK Authentication',
|
|
182
|
+
type: 'boolean',
|
|
183
|
+
required: false,
|
|
184
|
+
default: false,
|
|
185
|
+
description: 'Set to true to enable the SDK Authentication feature.'
|
|
186
|
+
},
|
|
187
|
+
inAppMessageZIndex: {
|
|
188
|
+
label: 'In-App Message Z Index',
|
|
189
|
+
type: 'number',
|
|
190
|
+
required: false,
|
|
191
|
+
description:
|
|
192
|
+
"By default, the Braze SDK will show In-App Messages with a z-index of 1040 for the screen overlay, 1050 for the actual in-app message, and 1060 for the message's close button. Provide a value for this option to override these default z-indexes."
|
|
193
|
+
},
|
|
194
|
+
localization: {
|
|
195
|
+
label: 'Localization',
|
|
196
|
+
type: 'string',
|
|
197
|
+
default: 'en',
|
|
198
|
+
required: false,
|
|
199
|
+
description:
|
|
200
|
+
"By default, any SDK-generated user-visible messages will be displayed in the user's browser language. Provide a value for this option to override that behavior and force a specific language. The value for this option should be a ISO 639-1 Language Code."
|
|
201
|
+
},
|
|
202
|
+
automaticallyDisplayMessages: {
|
|
203
|
+
label: 'Automatically Send In-App Messages',
|
|
204
|
+
type: 'boolean',
|
|
205
|
+
default: true,
|
|
206
|
+
required: false,
|
|
207
|
+
description:
|
|
208
|
+
"When this is enabled, all In-App Messages that a user is eligible for are automatically delivered to the user. If you'd like to register your own display subscribers or send soft push notifications to your users, make sure to disable this option."
|
|
209
|
+
},
|
|
210
|
+
manageServiceWorkerExternally: {
|
|
211
|
+
label: 'Manage Service Worker Externally',
|
|
212
|
+
type: 'boolean',
|
|
213
|
+
default: false,
|
|
214
|
+
required: false,
|
|
215
|
+
description:
|
|
216
|
+
'If you have your own service worker that you register and control the lifecycle of, set this option to true and the Braze SDK will not register or unregister a service worker. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)'
|
|
217
|
+
},
|
|
218
|
+
minimumIntervalBetweenTriggerActionsInSeconds: {
|
|
219
|
+
label: 'Minimum Interval Between Trigger Actions in Seconds',
|
|
220
|
+
type: 'number',
|
|
221
|
+
required: false,
|
|
222
|
+
default: 30,
|
|
223
|
+
description:
|
|
224
|
+
'Provide a value to override the default interval between trigger actions with a value of your own. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)'
|
|
225
|
+
},
|
|
226
|
+
noCookies: {
|
|
227
|
+
label: 'No Cookies',
|
|
228
|
+
type: 'boolean',
|
|
229
|
+
default: false,
|
|
230
|
+
required: false,
|
|
231
|
+
description:
|
|
232
|
+
'By default, the Braze SDK will store small amounts of data (user ids, session ids), in cookies. Pass true for this option to disable cookie storage and rely entirely on HTML 5 localStorage to identify users and sessions. [See more details](https://js.appboycdn.com/web-sdk/latest/doc/modules/appboy.html#initializationoptions)'
|
|
233
|
+
},
|
|
234
|
+
openCardsInNewTab: {
|
|
235
|
+
label: 'Open Cards In New Tab',
|
|
236
|
+
type: 'boolean',
|
|
237
|
+
default: false,
|
|
238
|
+
required: false,
|
|
239
|
+
description:
|
|
240
|
+
'By default, links from Card objects load in the current tab or window. Set this option to true to make links from cards open in a new tab or window.'
|
|
241
|
+
},
|
|
242
|
+
openInAppMessagesInNewTab: {
|
|
243
|
+
label: 'Open In-App Messages In New Tab',
|
|
244
|
+
type: 'boolean',
|
|
245
|
+
default: false,
|
|
246
|
+
required: false,
|
|
247
|
+
description:
|
|
248
|
+
'By default, links from in-app message clicks load in the current tab or a new tab as specified in the dashboard on a message-by-message basis. Set this option to true to force all links from in-app message clicks open in a new tab or window.'
|
|
249
|
+
},
|
|
250
|
+
requireExplicitInAppMessageDismissal: {
|
|
251
|
+
label: 'Require Explicit In-App Message Dismissal',
|
|
252
|
+
type: 'boolean',
|
|
253
|
+
required: false,
|
|
254
|
+
default: false,
|
|
255
|
+
description:
|
|
256
|
+
'By default, when an in-app message is showing, pressing the escape button or a click on the greyed-out background of the page will dismiss the message. Set this option to true to prevent this behavior and require an explicit button click to dismiss messages.'
|
|
257
|
+
},
|
|
258
|
+
safariWebsitePushId: {
|
|
259
|
+
label: 'Safari Website Push ID',
|
|
260
|
+
type: 'string',
|
|
261
|
+
required: false,
|
|
262
|
+
description:
|
|
263
|
+
'If you support Safari push, you must specify this option with the website push ID that you provided to Apple when creating your Safari push certificate (starts with "web", e.g. "web.com.example.domain").'
|
|
264
|
+
},
|
|
265
|
+
serviceWorkerLocation: {
|
|
266
|
+
label: 'Service Worker Location',
|
|
267
|
+
type: 'string',
|
|
268
|
+
required: false,
|
|
269
|
+
description:
|
|
270
|
+
'By default, when registering users for web push notifications Braze will look for the required service worker file in the root directory of your web server at /service-worker.js. If you want to host your service worker at a different path on that server, provide a value for this option that is the absolute path to the file, e.g. /mycustompath/my-worker.js. VERY IMPORTANT: setting a value here limits the scope of push notifications on your site. For instance, in the above example, because the service ,worker file is located within the /mycustompath/ directory, appboy.registerAppboyPushMessages MAY ONLY BE CALLED from web pages that start with http://yoursite.com/mycustompath/.'
|
|
271
|
+
},
|
|
272
|
+
sessionTimeoutInSeconds: {
|
|
273
|
+
label: 'Session Timeout in Seconds',
|
|
274
|
+
type: 'number',
|
|
275
|
+
default: 1800, // 30 minutes
|
|
276
|
+
required: false,
|
|
277
|
+
description:
|
|
278
|
+
'By default, sessions time out after 30 minutes of inactivity. Provide a value for this configuration option to override that default with a value of your own.'
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
initialize: async ({ settings, analytics }, dependencies) => {
|
|
282
|
+
try {
|
|
283
|
+
const {
|
|
284
|
+
endpoint,
|
|
285
|
+
api_key,
|
|
286
|
+
sdkVersion,
|
|
287
|
+
automaticallyDisplayMessages,
|
|
288
|
+
// @ts-expect-error versionSettings is not part of the settings object but they are injected by Analytics 2.0, making Braze SDK raise a warning when we initialize it.
|
|
289
|
+
versionSettings,
|
|
290
|
+
// @ts-expect-error same as above.
|
|
291
|
+
subscriptions,
|
|
292
|
+
deferUntilIdentified,
|
|
293
|
+
...expectedConfig
|
|
294
|
+
} = settings
|
|
295
|
+
|
|
296
|
+
const version = sdkVersion ?? defaultVersion
|
|
297
|
+
|
|
298
|
+
resetUserCache()
|
|
299
|
+
|
|
300
|
+
if (version.indexOf('3.') === 0) {
|
|
301
|
+
await dependencies.loadScript(`https://js.appboycdn.com/web-sdk/${version}/appboy.no-amd.min.js`)
|
|
302
|
+
} else {
|
|
303
|
+
await dependencies.loadScript(`https://js.appboycdn.com/web-sdk/${version}/braze.no-module.min.js`)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
let initialized = false
|
|
307
|
+
|
|
308
|
+
const client: BrazeDestinationClient = {
|
|
309
|
+
instance: version.indexOf('3.') === 0 ? window.appboy : window.braze,
|
|
310
|
+
ready: () => {
|
|
311
|
+
if (initialized) {
|
|
312
|
+
return true
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (deferUntilIdentified && typeof analytics.user().id() !== 'string') {
|
|
316
|
+
return false
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
client.instance.initialize(api_key, {
|
|
320
|
+
baseUrl: window.BRAZE_BASE_URL || endpoint,
|
|
321
|
+
...expectedConfig
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
if (typeof client.instance.addSdkMetadata === 'function') {
|
|
325
|
+
client.instance.addSdkMetadata([client.instance.BrazeSdkMetadata.SEGMENT])
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (automaticallyDisplayMessages) {
|
|
329
|
+
if ('display' in client.instance) {
|
|
330
|
+
client.instance.display.automaticallyShowNewInAppMessages()
|
|
331
|
+
} else {
|
|
332
|
+
client.instance.automaticallyShowInAppMessages()
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
client.instance.openSession()
|
|
337
|
+
|
|
338
|
+
return (initialized = true)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
client.ready()
|
|
343
|
+
|
|
344
|
+
return client
|
|
345
|
+
} catch (e) {
|
|
346
|
+
throw new Error(`Failed to initialize Braze ${e}`)
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
presets,
|
|
350
|
+
actions: {
|
|
351
|
+
updateUserProfile,
|
|
352
|
+
trackEvent,
|
|
353
|
+
trackPurchase,
|
|
354
|
+
debounce
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export default browserDestination(destination)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Generated file. DO NOT MODIFY IT BY HAND.
|
|
2
|
+
|
|
3
|
+
export interface Payload {
|
|
4
|
+
/**
|
|
5
|
+
* The identifier for the event to track.
|
|
6
|
+
*/
|
|
7
|
+
eventName: string
|
|
8
|
+
/**
|
|
9
|
+
* Hash of properties for this event.
|
|
10
|
+
*/
|
|
11
|
+
eventProperties?: {
|
|
12
|
+
[k: string]: unknown
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
|
|
2
|
+
import type { Settings } from '../generated-types'
|
|
3
|
+
import type { Payload } from './generated-types'
|
|
4
|
+
import { BrazeDestinationClient } from '../braze-types'
|
|
5
|
+
|
|
6
|
+
const action: BrowserActionDefinition<Settings, BrazeDestinationClient, Payload> = {
|
|
7
|
+
title: 'Track Event',
|
|
8
|
+
description: 'Reports that the current user performed a custom named event.',
|
|
9
|
+
defaultSubscription: 'type = "track" and event != "Order Completed"',
|
|
10
|
+
platform: 'web',
|
|
11
|
+
fields: {
|
|
12
|
+
eventName: {
|
|
13
|
+
type: 'string',
|
|
14
|
+
required: true,
|
|
15
|
+
description: 'The identifier for the event to track.',
|
|
16
|
+
label: 'Event Name',
|
|
17
|
+
default: {
|
|
18
|
+
'@path': '$.event'
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
eventProperties: {
|
|
22
|
+
type: 'object',
|
|
23
|
+
required: false,
|
|
24
|
+
description: 'Hash of properties for this event.',
|
|
25
|
+
label: 'Event Properties',
|
|
26
|
+
default: {
|
|
27
|
+
'@path': '$.properties'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
perform: (client, event) => {
|
|
32
|
+
if (!client.ready()) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
client.instance.logCustomEvent(event.payload.eventName, event.payload.eventProperties)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default action
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Generated file. DO NOT MODIFY IT BY HAND.
|
|
2
|
+
|
|
3
|
+
export interface Payload {
|
|
4
|
+
/**
|
|
5
|
+
* Hash of properties for this purchase. Keys are limited to 255 characters in length, cannot begin with a $, and can only contain alphanumeric characters and punctuation. Values can be numeric, boolean, Date objects, strings 255 characters or shorter, or nested objects whose values can be numeric, boolean, Date objects, arrays, strings, or null. Total size of purchase properties cannot exceed 50KB.
|
|
6
|
+
*/
|
|
7
|
+
purchaseProperties?: {
|
|
8
|
+
[k: string]: unknown
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* List of products purchased by the user
|
|
12
|
+
*/
|
|
13
|
+
products?: {
|
|
14
|
+
/**
|
|
15
|
+
* A string identifier for the product purchased, e.g. an SKU. Value is limited to 255 characters in length, cannot begin with a $, and can only contain alphanumeric characters and punctuation.
|
|
16
|
+
*/
|
|
17
|
+
product_id: string
|
|
18
|
+
/**
|
|
19
|
+
* The price paid. Base units depend on the currency. As an example, USD should be reported as Dollars.Cents, whereas JPY should be reported as a whole number of Yen. All provided values will be rounded to two digits with toFixed(2)
|
|
20
|
+
*/
|
|
21
|
+
price: number
|
|
22
|
+
/**
|
|
23
|
+
* Default USD. Currencies should be represented as an ISO 4217 currency code
|
|
24
|
+
*/
|
|
25
|
+
currency?: string
|
|
26
|
+
/**
|
|
27
|
+
* Default 1. The quantity of items purchased expressed as a whole number. Must be at least 1 and at most 100.
|
|
28
|
+
*/
|
|
29
|
+
quantity?: number
|
|
30
|
+
}[]
|
|
31
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { BrazeDestinationClient } from '../braze-types'
|
|
2
|
+
import { omit } from '@segment/actions-core'
|
|
3
|
+
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
|
|
4
|
+
import type { Settings } from '../generated-types'
|
|
5
|
+
import type { Payload } from './generated-types'
|
|
6
|
+
|
|
7
|
+
const action: BrowserActionDefinition<Settings, BrazeDestinationClient, Payload> = {
|
|
8
|
+
title: 'Track Purchase',
|
|
9
|
+
defaultSubscription: 'type = "track" and event = "Order Completed"',
|
|
10
|
+
description: 'Reports that the current user made an in-app purchase.',
|
|
11
|
+
platform: 'web',
|
|
12
|
+
fields: {
|
|
13
|
+
purchaseProperties: {
|
|
14
|
+
label: 'Purchase Properties',
|
|
15
|
+
type: 'object',
|
|
16
|
+
description: `Hash of properties for this purchase. Keys are limited to 255 characters in length, cannot begin with a $, and can only contain alphanumeric characters and punctuation. Values can be numeric, boolean, Date objects, strings 255 characters or shorter, or nested objects whose values can be numeric, boolean, Date objects, arrays, strings, or null. Total size of purchase properties cannot exceed 50KB.`,
|
|
17
|
+
default: {
|
|
18
|
+
'@path': '$.properties'
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
products: {
|
|
22
|
+
label: 'Products',
|
|
23
|
+
description: 'List of products purchased by the user',
|
|
24
|
+
properties: {
|
|
25
|
+
product_id: {
|
|
26
|
+
label: 'Product ID',
|
|
27
|
+
type: 'string',
|
|
28
|
+
required: true,
|
|
29
|
+
description: `A string identifier for the product purchased, e.g. an SKU. Value is limited to 255 characters in length, cannot begin with a $, and can only contain alphanumeric characters and punctuation.`
|
|
30
|
+
},
|
|
31
|
+
price: {
|
|
32
|
+
label: 'Price',
|
|
33
|
+
type: 'number',
|
|
34
|
+
required: true,
|
|
35
|
+
description: `The price paid. Base units depend on the currency. As an example, USD should be reported as Dollars.Cents, whereas JPY should be reported as a whole number of Yen. All provided values will be rounded to two digits with toFixed(2)`
|
|
36
|
+
},
|
|
37
|
+
currency: {
|
|
38
|
+
label: 'Currency Code',
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: `Default USD. Currencies should be represented as an ISO 4217 currency code`
|
|
41
|
+
},
|
|
42
|
+
quantity: {
|
|
43
|
+
label: 'Quantity',
|
|
44
|
+
type: 'number',
|
|
45
|
+
description: `Default 1. The quantity of items purchased expressed as a whole number. Must be at least 1 and at most 100.`
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
type: 'object',
|
|
49
|
+
multiple: true,
|
|
50
|
+
default: {
|
|
51
|
+
'@path': '$.properties.products'
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
perform: (client, data) => {
|
|
56
|
+
if (!client.ready()) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const payload = data.payload
|
|
61
|
+
|
|
62
|
+
const reservedKeys = Object.keys(action.fields.products.properties ?? {})
|
|
63
|
+
const purchaseProperties = omit(payload.purchaseProperties, reservedKeys)
|
|
64
|
+
|
|
65
|
+
if (purchaseProperties?.products && Array.isArray(purchaseProperties?.products)) {
|
|
66
|
+
purchaseProperties?.products?.forEach((product) => {
|
|
67
|
+
const result = client.instance.logPurchase(
|
|
68
|
+
(product.product_id as string | number).toString(),
|
|
69
|
+
product.price,
|
|
70
|
+
product.currency ?? 'USD',
|
|
71
|
+
product.quantity ?? 1,
|
|
72
|
+
purchaseProperties
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
if (!result) {
|
|
76
|
+
console.warn('Braze failed to attach purchase to the session for product ', product.productId)
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default action
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Generated file. DO NOT MODIFY IT BY HAND.
|
|
2
|
+
|
|
3
|
+
export interface Payload {
|
|
4
|
+
/**
|
|
5
|
+
* The unique user identifier
|
|
6
|
+
*/
|
|
7
|
+
external_id?: string
|
|
8
|
+
/**
|
|
9
|
+
* The country code of the user
|
|
10
|
+
*/
|
|
11
|
+
country?: string | null
|
|
12
|
+
/**
|
|
13
|
+
* The user's current longitude/latitude.
|
|
14
|
+
*/
|
|
15
|
+
current_location?: {
|
|
16
|
+
key: string
|
|
17
|
+
latitude: number
|
|
18
|
+
longitude: number
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sets a custom user attribute. This can be any key/value pair and is used to collect extra information about the user.
|
|
22
|
+
*/
|
|
23
|
+
custom_attributes?: {
|
|
24
|
+
[k: string]: unknown
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The user's date of birth
|
|
28
|
+
*/
|
|
29
|
+
dob?: string | number | null
|
|
30
|
+
/**
|
|
31
|
+
* The user's email
|
|
32
|
+
*/
|
|
33
|
+
email?: string | null
|
|
34
|
+
/**
|
|
35
|
+
* The user's email subscription preference: “opted_in” (explicitly registered to receive email messages), “unsubscribed” (explicitly opted out of email messages), and “subscribed” (neither opted in nor out).
|
|
36
|
+
*/
|
|
37
|
+
email_subscribe?: string
|
|
38
|
+
/**
|
|
39
|
+
* The user's first name
|
|
40
|
+
*/
|
|
41
|
+
first_name?: string | null
|
|
42
|
+
/**
|
|
43
|
+
* The user's last name
|
|
44
|
+
*/
|
|
45
|
+
last_name?: string
|
|
46
|
+
/**
|
|
47
|
+
* The user's gender: “M”, “F”, “O” (other), “N” (not applicable), “P” (prefer not to say) or nil (unknown).
|
|
48
|
+
*/
|
|
49
|
+
gender?: string | null
|
|
50
|
+
/**
|
|
51
|
+
* The user's home city.
|
|
52
|
+
*/
|
|
53
|
+
home_city?: string | null
|
|
54
|
+
/**
|
|
55
|
+
* URL of image to be associated with user profile.
|
|
56
|
+
*/
|
|
57
|
+
image_url?: string
|
|
58
|
+
/**
|
|
59
|
+
* The user's preferred language.
|
|
60
|
+
*/
|
|
61
|
+
language?: string | null
|
|
62
|
+
/**
|
|
63
|
+
* The user's phone number
|
|
64
|
+
*/
|
|
65
|
+
phone?: string | null
|
|
66
|
+
/**
|
|
67
|
+
* The user's push subscription preference: “opted_in” (explicitly registered to receive push messages), “unsubscribed” (explicitly opted out of push messages), and “subscribed” (neither opted in nor out).
|
|
68
|
+
*/
|
|
69
|
+
push_subscribe?: string
|
|
70
|
+
}
|