@segment/analytics-browser-actions-facebook-conversions-api-web 1.8.1-staging-701e5e031.0 → 1.9.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/src/functions.ts CHANGED
@@ -1,52 +1,64 @@
1
- import { WindowWithOptionalFbq, InitOptions, LDU, UserData, FBClient } from './types'
1
+ import { WindowWithOptionalFbq, InitOptions, LDU, UserData, FBClient} from './types'
2
2
  import type { Settings } from './generated-types'
3
- import { USER_DATA_KEY, INIT_COUNT_KEY } from './constants'
3
+ import { USER_DATA_KEY, INIT_COUNT_KEY} from './constants'
4
4
  import { UniversalStorage, Analytics } from '@segment/analytics-next'
5
5
 
6
6
  export function initScript(settings: Settings, analytics: Analytics) {
7
- const { pixelId, disablePushState, disableAutoConfig, disableFirstPartyCookies, agent, ldu } =
8
- settings as Settings & { ldu: keyof typeof LDU }
9
-
10
- ;(function (
11
- f: WindowWithOptionalFbq,
12
- b: Document,
13
- e: 'script',
14
- v: string,
15
- n: FBClient | undefined = undefined,
16
- t: HTMLScriptElement | undefined = undefined,
7
+ const {
8
+ pixelId,
9
+ disablePushState,
10
+ disableAutoConfig,
11
+ disableFirstPartyCookies,
12
+ agent,
13
+ ldu
14
+ } = settings as Settings & { ldu: keyof typeof LDU }
15
+
16
+ (function(
17
+ f: WindowWithOptionalFbq,
18
+ b: Document,
19
+ e: 'script',
20
+ v: string,
21
+ n: FBClient | undefined = undefined,
22
+ t: HTMLScriptElement | undefined = undefined,
17
23
  s: Element | null = null
18
- ) {
19
- if (f.fbq) return
20
- n = f.fbq = function () {
24
+ ){
25
+ if (f.fbq) return;
26
+ n = f.fbq = function() {
21
27
  /* eslint-disable */
22
28
  // @ts-expect-error - n is defined by the time this executes
23
- n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments)
29
+ n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments);
24
30
  /* eslint-enable */
25
- }
26
- if (!f._fbq) f._fbq = n
27
- n.push = n
28
- n.loaded = true
29
- n.version = '2.0'
30
- n.queue = []
31
- t = b.createElement(e)
32
- t.async = true
33
- t.src = v
34
- s = b.getElementsByTagName(e)[0]
31
+ };
32
+ if (!f._fbq) f._fbq = n;
33
+ n.push = n;
34
+ n.loaded = true;
35
+ n.version = '2.0';
36
+ n.queue = [];
37
+ t = b.createElement(e);
38
+ t.async = true;
39
+ t.src = v;
40
+ s = b.getElementsByTagName(e)[0];
35
41
  if (s && s.parentNode) {
36
- s.parentNode.insertBefore(t, s)
42
+ s.parentNode.insertBefore(t, s);
37
43
  }
38
- })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js')
39
-
40
- if (ldu === LDU.Disabled.key) {
44
+ })(
45
+ window,
46
+ document,
47
+ 'script',
48
+ 'https://connect.facebook.net/en_US/fbevents.js'
49
+ );
50
+
51
+ if(ldu === LDU.Disabled.key) {
41
52
  window.fbq('dataProcessingOptions', [])
42
- } else {
53
+ }
54
+ else {
43
55
  const lduObj = getLDU(ldu)
44
56
  window.fbq('dataProcessingOptions', ['LDU'], lduObj.country, lduObj.state)
45
57
  }
46
58
 
47
- if (disablePushState) {
59
+ if(disablePushState) {
48
60
  window.fbq.disablePushState = true
49
- }
61
+ }
50
62
 
51
63
  if (disableAutoConfig) {
52
64
  window.fbq('set', 'autoConfig', false, pixelId)
@@ -57,13 +69,13 @@ export function initScript(settings: Settings, analytics: Analytics) {
57
69
  }
58
70
 
59
71
  const userData = getStoredUserData(analytics)
60
- const options: InitOptions | undefined = agent ? { agent } : undefined
72
+ const options: InitOptions | undefined = ( agent ? { agent } : undefined)
61
73
  const initArgs: [string, UserData?, InitOptions?] = [pixelId]
62
74
 
63
75
  if (userData && Object.keys(userData).length > 0) {
64
- initArgs.push(userData)
76
+ initArgs.push(userData);
65
77
  if (options) {
66
- initArgs.push(options)
78
+ initArgs.push(options);
67
79
  }
68
80
  } else if (options) {
69
81
  initArgs.push(undefined, options)
@@ -74,7 +86,7 @@ export function initScript(settings: Settings, analytics: Analytics) {
74
86
  setStorageInitCount(analytics, 1)
75
87
  deleteStorageUserData(analytics)
76
88
 
77
- if (!disablePushState) {
89
+ if(!disablePushState) {
78
90
  window.fbq('trackSingle', pixelId, 'PageView')
79
91
  }
80
92
  }
@@ -106,29 +118,24 @@ export function deleteStorageUserData(analytics: Analytics) {
106
118
  storage.set(USER_DATA_KEY, '')
107
119
  }
108
120
 
109
- function getStoredUserData(analytics: Analytics): UserData | undefined {
110
- const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
111
- const userDataFromStorage: string | null = storage.get(USER_DATA_KEY)
112
- if (userDataFromStorage) {
113
- try {
114
- const parsed = JSON.parse(userDataFromStorage)
115
- if (
116
- !parsed ||
117
- typeof parsed !== 'object' ||
118
- Array.isArray(parsed) ||
119
- Object.keys(parsed as object).length === 0
120
- ) {
121
- return undefined
122
- }
123
- return parsed as UserData
124
- } catch {
125
- return undefined
121
+ function getStoredUserData(analytics: Analytics): UserData | undefined {
122
+ const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
123
+ const userDataFromStorage: string | null = storage.get(USER_DATA_KEY)
124
+ if(userDataFromStorage) {
125
+ try {
126
+ const parsed = JSON.parse(userDataFromStorage)
127
+ if(!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || Object.keys(parsed as object).length === 0) {
128
+ return undefined
129
+ }
130
+ return parsed as UserData
131
+ } catch {
132
+ return undefined
133
+ }
126
134
  }
127
- }
128
- return undefined
135
+ return undefined
129
136
  }
130
137
 
131
138
  function getLDU(ldu: keyof typeof LDU) {
132
- const lduObj = LDU[ldu]
133
- return { country: lduObj.country, state: lduObj.state }
134
- }
139
+ const lduObj = LDU[ldu]
140
+ return { country: lduObj.country, state: lduObj.state }
141
+ }
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ import { defaultValues } from '@segment/actions-core'
8
8
 
9
9
  declare global {
10
10
  interface Window {
11
- fbq: FBClient
11
+ fbq: FBClient,
12
12
  _fbq: FBClient
13
13
  }
14
14
  }
@@ -23,25 +23,22 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
23
23
  description: 'The Pixel ID associated with your Facebook Pixel.',
24
24
  label: 'Pixel ID',
25
25
  type: 'string',
26
- required: true
26
+ required: true
27
27
  },
28
28
  disablePushState: {
29
- description:
30
- 'If set to true, prevents Facebook Pixel from sending PageView events on history state changes. Set to true if you want to trigger PageView events manually via the pageView Action.',
29
+ description: "If set to true, prevents Facebook Pixel from sending PageView events on history state changes. Set to true if you want to trigger PageView events manually via the pageView Action.",
31
30
  label: 'Disable Push State',
32
31
  type: 'boolean',
33
32
  default: false
34
33
  },
35
34
  disableAutoConfig: {
36
- description:
37
- 'Control whether Facebook’s Meta Pixel automatically collects additional page and button data to optimize ads and measurement. When this toggle is on, Auto Config is disabled and only basic pixel tracking will occur. Turning it off enables Auto Config, allowing the Pixel to automatically send page metadata and button interactions to improve ad delivery and reporting.',
35
+ description: "Control whether Facebook’s Meta Pixel automatically collects additional page and button data to optimize ads and measurement. When this toggle is on, Auto Config is disabled and only basic pixel tracking will occur. Turning it off enables Auto Config, allowing the Pixel to automatically send page metadata and button interactions to improve ad delivery and reporting.",
38
36
  label: 'Disable Auto Config',
39
37
  type: 'boolean',
40
38
  default: true
41
39
  },
42
40
  disableFirstPartyCookies: {
43
- description:
44
- 'Control whether Facebook’s Meta Pixel uses first-party cookies. When this toggle is on, first-party cookies are disabled, enhancing user privacy. Turning it off enables the use of first-party cookies for more accurate tracking.',
41
+ description: "Control whether Facebook’s Meta Pixel uses first-party cookies. When this toggle is on, first-party cookies are disabled, enhancing user privacy. Turning it off enables the use of first-party cookies for more accurate tracking.",
45
42
  label: 'Disable First Party Cookies',
46
43
  type: 'boolean',
47
44
  default: false
@@ -58,8 +55,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
58
55
  type: 'string',
59
56
  required: true,
60
57
  choices: [
61
- { label: 'LDU disabled', value: LDU.Disabled.key },
62
- { label: 'LDU enabled - Use Meta Geolocation Logic', value: LDU.GeolocationLogic.key },
58
+ { label: 'LDU disabled', value: LDU.Disabled.key},
59
+ { label: "LDU enabled - Use Meta Geolocation Logic", value: LDU.GeolocationLogic.key },
63
60
  { label: 'LDU enabled - California only', value: LDU.California.key },
64
61
  { label: 'LDU enabled - Colorado only', value: LDU.Colorado.key },
65
62
  { label: 'LDU enabled - Connecticut only', value: LDU.Connecticut.key },
@@ -90,7 +87,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
90
87
  name: 'AddPaymentInfo',
91
88
  subscribe: 'event = "Payment Info Entered"',
92
89
  partnerAction: 'send',
93
- mapping: {
90
+ mapping:
91
+ {
94
92
  ...defaultValues(send.fields),
95
93
  event_config: {
96
94
  event_name: 'AddPaymentInfo',
@@ -103,7 +101,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
103
101
  name: 'AddToCart',
104
102
  subscribe: 'event = "Product Added"',
105
103
  partnerAction: 'send',
106
- mapping: {
104
+ mapping:
105
+ {
107
106
  ...defaultValues(send.fields),
108
107
  event_config: {
109
108
  event_name: 'AddToCart',
@@ -118,12 +117,13 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
118
117
  value: { '@path': '$.properties.price' }
119
118
  },
120
119
  type: 'automatic'
121
- },
120
+ },
122
121
  {
123
122
  name: 'AddToWishlist',
124
123
  subscribe: 'event = "Product Added To Wishlist"',
125
124
  partnerAction: 'send',
126
- mapping: {
125
+ mapping:
126
+ {
127
127
  ...defaultValues(send.fields),
128
128
  event_config: {
129
129
  event_name: 'AddToWishlist',
@@ -143,7 +143,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
143
143
  name: 'CompleteRegistration',
144
144
  subscribe: 'event = "Signed Up"',
145
145
  partnerAction: 'send',
146
- mapping: {
146
+ mapping:
147
+ {
147
148
  ...defaultValues(send.fields),
148
149
  event_config: {
149
150
  event_name: 'CompleteRegistration',
@@ -151,12 +152,13 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
151
152
  }
152
153
  },
153
154
  type: 'automatic'
154
- },
155
+ },
155
156
  {
156
157
  name: 'InitiateCheckout',
157
158
  subscribe: 'event = "Checkout Started"',
158
159
  partnerAction: 'send',
159
- mapping: {
160
+ mapping:
161
+ {
160
162
  ...defaultValues(send.fields),
161
163
  event_config: {
162
164
  event_name: 'InitiateCheckout',
@@ -184,13 +186,14 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
184
186
  name: 'Purchase',
185
187
  subscribe: 'event = "Order Completed"',
186
188
  partnerAction: 'send',
187
- mapping: {
189
+ mapping:
190
+ {
188
191
  ...defaultValues(send.fields),
189
192
  event_config: {
190
193
  event_name: 'Purchase',
191
194
  show_fields: false
192
195
  },
193
- value: { '@path': '$.properties.revenue' },
196
+ value: { '@path': '$.properties.revenue' },
194
197
  custom_data: {
195
198
  order_id: { '@path': '$.properties.order_id' }
196
199
  }
@@ -201,7 +204,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
201
204
  name: 'Search',
202
205
  subscribe: 'event = "Products Searched"',
203
206
  partnerAction: 'send',
204
- mapping: {
207
+ mapping:
208
+ {
205
209
  ...defaultValues(send.fields),
206
210
  event_config: {
207
211
  event_name: 'Search',
@@ -220,7 +224,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
220
224
  name: 'ViewContent',
221
225
  subscribe: 'event = "Product Viewed"',
222
226
  partnerAction: 'send',
223
- mapping: {
227
+ mapping:
228
+ {
224
229
  ...defaultValues(send.fields),
225
230
  event_config: {
226
231
  event_name: 'ViewContent',
@@ -230,7 +235,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
230
235
  id: { '@path': '$.properties.product_id' },
231
236
  quantity: { '@path': '$.properties.quantity' },
232
237
  item_price: { '@path': '$.properties.price' }
233
- },
238
+ },
234
239
  content_ids: { '@path': '$.properties.product_id' },
235
240
  value: { '@path': '$.properties.price' }
236
241
  },
@@ -2,79 +2,47 @@ import { DependsOnConditions, Condition } from '@segment/actions-core/destinatio
2
2
  import type { FBStandardEventType, FBNonStandardEventType } from '../types'
3
3
 
4
4
  export const fieldDependencies: Record<string, (FBStandardEventType | FBNonStandardEventType)[]> = {
5
- custom_event_name: ['CustomEvent'],
6
- content_category: ['PageView', 'ViewContent', 'Search'],
7
- content_ids: [
8
- 'AddPaymentInfo',
9
- 'AddToCart',
10
- 'AddToWishlist',
11
- 'InitiateCheckout',
12
- 'Purchase',
13
- 'Search',
14
- 'ViewContent'
15
- ],
16
- content_name: ['PageView', 'ViewContent', 'Search'],
17
- content_type: ['AddToCart', 'Purchase', 'Search', 'ViewContent'],
18
- contents: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'InitiateCheckout', 'Purchase', 'Search', 'ViewContent'],
19
- currency: [
20
- 'AddPaymentInfo',
21
- 'AddToCart',
22
- 'AddToWishlist',
23
- 'CompleteRegistration',
24
- 'InitiateCheckout',
25
- 'Lead',
26
- 'Purchase',
27
- 'Search',
28
- 'StartTrial',
29
- 'Subscribe',
30
- 'ViewContent'
31
- ],
32
- delivery_category: ['Purchase', 'InitiateCheckout'],
33
- num_items: ['InitiateCheckout'],
34
- predicted_ltv: ['Purchase', 'Subscribe', 'StartTrial', 'CompleteRegistration', 'AddPaymentInfo', 'CustomEvent'],
35
- net_revenue: ['Purchase'],
36
- search_string: ['Search'],
37
- status: ['CompleteRegistration'],
38
- value: [
39
- 'AddPaymentInfo',
40
- 'AddToCart',
41
- 'AddToWishlist',
42
- 'CompleteRegistration',
43
- 'InitiateCheckout',
44
- 'Lead',
45
- 'Purchase',
46
- 'Search',
47
- 'StartTrial',
48
- 'Subscribe',
49
- 'ViewContent'
50
- ]
5
+ custom_event_name: ['CustomEvent'],
6
+ content_category: ['PageView', 'ViewContent', 'Search'],
7
+ content_ids: ['AddPaymentInfo','AddToCart','AddToWishlist','InitiateCheckout','Purchase','Search','ViewContent'],
8
+ content_name: ['PageView', 'ViewContent', 'Search'],
9
+ content_type: ['AddToCart', 'Purchase', 'Search', 'ViewContent'],
10
+ contents: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'InitiateCheckout', 'Purchase', 'Search', 'ViewContent'],
11
+ currency: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'CompleteRegistration', 'InitiateCheckout', 'Lead', 'Purchase', 'Search', 'StartTrial', 'Subscribe', 'ViewContent'],
12
+ delivery_category: ['Purchase', 'InitiateCheckout'],
13
+ num_items: ['InitiateCheckout'],
14
+ predicted_ltv: ['Purchase', 'Subscribe', 'StartTrial', 'CompleteRegistration', 'AddPaymentInfo', 'CustomEvent' ],
15
+ net_revenue: ['Purchase'],
16
+ search_string: ['Search'],
17
+ status: ['CompleteRegistration'],
18
+ value: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'CompleteRegistration', 'InitiateCheckout', 'Lead', 'Purchase', 'Search', 'StartTrial', 'Subscribe', 'ViewContent'],
51
19
  }
52
20
 
53
21
  export function getDependenciesFor(fieldName: string): DependsOnConditions {
54
22
  const conditions: Condition[] = [
55
- {
56
- fieldKey: 'show_fields',
57
- operator: 'is',
58
- value: 'true'
59
- }
23
+ {
24
+ fieldKey: 'event_config.show_fields',
25
+ operator: 'is',
26
+ value: 'true'
27
+ }
60
28
  ]
61
29
 
62
30
  if (fieldDependencies[fieldName]) {
63
- conditions.push({
64
- fieldKey: 'event_name',
65
- operator: 'is',
66
- value: fieldDependencies[fieldName]
67
- })
31
+ conditions.push({
32
+ fieldKey: 'event_config.event_name',
33
+ operator: 'is',
34
+ value: fieldDependencies[fieldName]
35
+ })
68
36
  }
69
37
 
70
38
  return {
71
- match: 'any',
72
- conditions
39
+ match: 'any',
40
+ conditions
73
41
  }
74
42
  }
75
43
 
76
44
  export function getNotVisibleForEvent(event: FBStandardEventType | FBNonStandardEventType): string[] {
77
- return Object.entries(fieldDependencies)
78
- .filter(([_, events]) => !events.includes(event))
79
- .map(([fieldName]) => fieldName)
80
- }
45
+ return Object.entries(fieldDependencies)
46
+ .filter(([_, events]) => !events.includes(event))
47
+ .map(([fieldName]) => fieldName)
48
+ }