@segment/analytics-browser-actions-google-analytics-4 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.
Files changed (64) hide show
  1. package/package.json +20 -0
  2. package/src/__tests__/addPaymentInfo.test.ts +101 -0
  3. package/src/__tests__/addToCart.test.ts +95 -0
  4. package/src/__tests__/addToWishlist.test.ts +95 -0
  5. package/src/__tests__/beginCheckout.test.ts +101 -0
  6. package/src/__tests__/customEvent.test.ts +70 -0
  7. package/src/__tests__/generateLead.test.ts +68 -0
  8. package/src/__tests__/login.test.ts +63 -0
  9. package/src/__tests__/purchase.test.ts +103 -0
  10. package/src/__tests__/refund.test.ts +103 -0
  11. package/src/__tests__/removeFromCart.test.ts +99 -0
  12. package/src/__tests__/search.test.ts +64 -0
  13. package/src/__tests__/selectItem.test.ts +96 -0
  14. package/src/__tests__/selectPromotion.test.ts +111 -0
  15. package/src/__tests__/signUp.test.ts +62 -0
  16. package/src/__tests__/viewCart.test.ts +96 -0
  17. package/src/__tests__/viewItem.test.ts +96 -0
  18. package/src/__tests__/viewItemList.test.ts +96 -0
  19. package/src/__tests__/viewPromotion.test.ts +111 -0
  20. package/src/addPaymentInfo/generated-types.ts +117 -0
  21. package/src/addPaymentInfo/index.ts +48 -0
  22. package/src/addToCart/generated-types.ts +109 -0
  23. package/src/addToCart/index.ts +36 -0
  24. package/src/addToWishlist/generated-types.ts +109 -0
  25. package/src/addToWishlist/index.ts +38 -0
  26. package/src/beginCheckout/generated-types.ts +113 -0
  27. package/src/beginCheckout/index.ts +38 -0
  28. package/src/customEvent/generated-types.ts +28 -0
  29. package/src/customEvent/index.ts +52 -0
  30. package/src/ga4-functions.ts +8 -0
  31. package/src/ga4-properties.ts +368 -0
  32. package/src/ga4-types.ts +28 -0
  33. package/src/generateLead/generated-types.ts +28 -0
  34. package/src/generateLead/index.ts +32 -0
  35. package/src/generated-types.ts +56 -0
  36. package/src/index.ts +194 -0
  37. package/src/login/generated-types.ts +24 -0
  38. package/src/login/index.ts +31 -0
  39. package/src/purchase/generated-types.ts +125 -0
  40. package/src/purchase/index.ts +54 -0
  41. package/src/refund/generated-types.ts +129 -0
  42. package/src/refund/index.ts +57 -0
  43. package/src/removeFromCart/generated-types.ts +109 -0
  44. package/src/removeFromCart/index.ts +37 -0
  45. package/src/search/generated-types.ts +24 -0
  46. package/src/search/index.ts +30 -0
  47. package/src/selectItem/generated-types.ts +109 -0
  48. package/src/selectItem/index.ts +43 -0
  49. package/src/selectPromotion/generated-types.ts +137 -0
  50. package/src/selectPromotion/index.ts +67 -0
  51. package/src/setConfigurationFields/generated-types.ts +70 -0
  52. package/src/setConfigurationFields/index.ts +143 -0
  53. package/src/signUp/generated-types.ts +24 -0
  54. package/src/signUp/index.ts +29 -0
  55. package/src/types.ts +3 -0
  56. package/src/viewCart/generated-types.ts +109 -0
  57. package/src/viewCart/index.ts +36 -0
  58. package/src/viewItem/generated-types.ts +109 -0
  59. package/src/viewItem/index.ts +37 -0
  60. package/src/viewItemList/generated-types.ts +109 -0
  61. package/src/viewItemList/index.ts +36 -0
  62. package/src/viewPromotion/generated-types.ts +137 -0
  63. package/src/viewPromotion/index.ts +67 -0
  64. package/tsconfig.json +9 -0
@@ -0,0 +1,109 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Payload {
4
+ /**
5
+ * A unique identifier for a user. See Google's [User-ID for cross-platform analysis](https://support.google.com/analytics/answer/9213390) and [Reporting: deduplicate user counts](https://support.google.com/analytics/answer/9355949?hl=en) documentation for more information on this identifier.
6
+ */
7
+ user_id?: string
8
+ /**
9
+ * Currency of the items associated with the event, in 3-letter ISO 4217 format.
10
+ */
11
+ currency?: string
12
+ /**
13
+ * The monetary value of the event.
14
+ */
15
+ value?: number
16
+ /**
17
+ * The list of products purchased.
18
+ */
19
+ items: {
20
+ /**
21
+ * Identifier for the product being purchased.
22
+ */
23
+ item_id?: string
24
+ /**
25
+ * Name of the product being purchased.
26
+ */
27
+ item_name?: string
28
+ /**
29
+ * A product affiliation to designate a supplying company or brick and mortar store location.
30
+ */
31
+ affiliation?: string
32
+ /**
33
+ * Coupon code used for a purchase.
34
+ */
35
+ coupon?: string
36
+ /**
37
+ * Currency of the purchase or items associated with the event, in 3-letter ISO 4217 format.
38
+ */
39
+ currency?: string
40
+ /**
41
+ * Monetary value of discount associated with a purchase.
42
+ */
43
+ discount?: number
44
+ /**
45
+ * The index/position of the item in a list.
46
+ */
47
+ index?: number
48
+ /**
49
+ * Brand associated with the product.
50
+ */
51
+ item_brand?: string
52
+ /**
53
+ * Product category.
54
+ */
55
+ item_category?: string
56
+ /**
57
+ * Product category 2.
58
+ */
59
+ item_category2?: string
60
+ /**
61
+ * Product category 3.
62
+ */
63
+ item_category3?: string
64
+ /**
65
+ * Product category 4.
66
+ */
67
+ item_category4?: string
68
+ /**
69
+ * Product category 5.
70
+ */
71
+ item_category5?: string
72
+ /**
73
+ * The ID of the list in which the item was presented to the user.
74
+ */
75
+ item_list_id?: string
76
+ /**
77
+ * The name of the list in which the item was presented to the user.
78
+ */
79
+ item_list_name?: string
80
+ /**
81
+ * Variant of the product (e.g. Black).
82
+ */
83
+ item_variant?: string
84
+ /**
85
+ * The location associated with the item.
86
+ */
87
+ location_id?: string
88
+ /**
89
+ * Price of the product being purchased, in units of the specified currency parameter.
90
+ */
91
+ price?: number
92
+ /**
93
+ * Item quantity.
94
+ */
95
+ quantity?: number
96
+ }[]
97
+ /**
98
+ * The user properties to send to Google Analytics 4. You must create user-scoped dimensions to ensure custom properties are picked up by Google. See Google’s [Custom user properties](https://support.google.com/analytics/answer/9269570) to learn how to set and register user properties.
99
+ */
100
+ user_properties?: {
101
+ [k: string]: unknown
102
+ }
103
+ /**
104
+ * The event parameters to send to Google Analytics 4.
105
+ */
106
+ params?: {
107
+ [k: string]: unknown
108
+ }
109
+ }
@@ -0,0 +1,38 @@
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
+
5
+ import { user_properties, params, value, currency, items_single_products, user_id } from '../ga4-properties'
6
+ import { updateUser } from '../ga4-functions'
7
+
8
+ // Change from unknown to the partner SDK types
9
+ const action: BrowserActionDefinition<Settings, Function, Payload> = {
10
+ title: 'Add to Wishlist',
11
+ description:
12
+ 'The event signifies that an item was added to a wishlist. Use this event to identify popular gift items in your app.',
13
+ platform: 'web',
14
+ defaultSubscription: 'type = "track" and event = "Product Added to Wishlist"',
15
+ fields: {
16
+ user_id: user_id,
17
+ currency: currency,
18
+ value: value,
19
+ items: {
20
+ ...items_single_products,
21
+ required: true
22
+ },
23
+ user_properties: user_properties,
24
+ params: params
25
+ },
26
+ perform: (gtag, { payload }) => {
27
+ updateUser(payload.user_id, payload.user_properties, gtag)
28
+
29
+ gtag('event', 'add_to_wishlist', {
30
+ currency: payload.currency,
31
+ value: payload.value,
32
+ items: payload.items,
33
+ ...payload.params
34
+ })
35
+ }
36
+ }
37
+
38
+ export default action
@@ -0,0 +1,113 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Payload {
4
+ /**
5
+ * A unique identifier for a user. See Google's [User-ID for cross-platform analysis](https://support.google.com/analytics/answer/9213390) and [Reporting: deduplicate user counts](https://support.google.com/analytics/answer/9355949?hl=en) documentation for more information on this identifier.
6
+ */
7
+ user_id?: string
8
+ /**
9
+ * Coupon code used for a purchase.
10
+ */
11
+ coupon?: string
12
+ /**
13
+ * Currency of the items associated with the event, in 3-letter ISO 4217 format.
14
+ */
15
+ currency?: string
16
+ /**
17
+ * The list of products purchased.
18
+ */
19
+ items: {
20
+ /**
21
+ * Identifier for the product being purchased.
22
+ */
23
+ item_id?: string
24
+ /**
25
+ * Name of the product being purchased.
26
+ */
27
+ item_name?: string
28
+ /**
29
+ * A product affiliation to designate a supplying company or brick and mortar store location.
30
+ */
31
+ affiliation?: string
32
+ /**
33
+ * Coupon code used for a purchase.
34
+ */
35
+ coupon?: string
36
+ /**
37
+ * Currency of the purchase or items associated with the event, in 3-letter ISO 4217 format.
38
+ */
39
+ currency?: string
40
+ /**
41
+ * Monetary value of discount associated with a purchase.
42
+ */
43
+ discount?: number
44
+ /**
45
+ * The index/position of the item in a list.
46
+ */
47
+ index?: number
48
+ /**
49
+ * Brand associated with the product.
50
+ */
51
+ item_brand?: string
52
+ /**
53
+ * Product category.
54
+ */
55
+ item_category?: string
56
+ /**
57
+ * Product category 2.
58
+ */
59
+ item_category2?: string
60
+ /**
61
+ * Product category 3.
62
+ */
63
+ item_category3?: string
64
+ /**
65
+ * Product category 4.
66
+ */
67
+ item_category4?: string
68
+ /**
69
+ * Product category 5.
70
+ */
71
+ item_category5?: string
72
+ /**
73
+ * The ID of the list in which the item was presented to the user.
74
+ */
75
+ item_list_id?: string
76
+ /**
77
+ * The name of the list in which the item was presented to the user.
78
+ */
79
+ item_list_name?: string
80
+ /**
81
+ * Variant of the product (e.g. Black).
82
+ */
83
+ item_variant?: string
84
+ /**
85
+ * The location associated with the item.
86
+ */
87
+ location_id?: string
88
+ /**
89
+ * Price of the product being purchased, in units of the specified currency parameter.
90
+ */
91
+ price?: number
92
+ /**
93
+ * Item quantity.
94
+ */
95
+ quantity?: number
96
+ }[]
97
+ /**
98
+ * The monetary value of the event.
99
+ */
100
+ value?: number
101
+ /**
102
+ * The event parameters to send to Google Analytics 4.
103
+ */
104
+ params?: {
105
+ [k: string]: unknown
106
+ }
107
+ /**
108
+ * The user properties to send to Google Analytics 4. You must create user-scoped dimensions to ensure custom properties are picked up by Google. See Google’s [Custom user properties](https://support.google.com/analytics/answer/9269570) to learn how to set and register user properties.
109
+ */
110
+ user_properties?: {
111
+ [k: string]: unknown
112
+ }
113
+ }
@@ -0,0 +1,38 @@
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
+
5
+ import { params, coupon, currency, value, items_multi_products, user_id, user_properties } from '../ga4-properties'
6
+ import { updateUser } from '../ga4-functions'
7
+
8
+ const action: BrowserActionDefinition<Settings, Function, Payload> = {
9
+ title: 'Begin Checkout',
10
+ description: 'This event signifies that a user has begun a checkout.',
11
+ defaultSubscription: 'type = "track" and event = "Checkout Started"',
12
+ platform: 'web',
13
+ fields: {
14
+ user_id: user_id,
15
+ coupon: { ...coupon, default: { '@path': '$.properties.coupon' } },
16
+ currency: currency,
17
+ items: {
18
+ ...items_multi_products,
19
+ required: true
20
+ },
21
+ value: value,
22
+ params: params,
23
+ user_properties: user_properties
24
+ },
25
+ perform: (gtag, { payload }) => {
26
+ updateUser(payload.user_id, payload.user_properties, gtag)
27
+
28
+ gtag('event', 'begin_checkout', {
29
+ currency: payload.currency,
30
+ value: payload.value,
31
+ coupon: payload.coupon,
32
+ items: payload.items,
33
+ ...payload.params
34
+ })
35
+ }
36
+ }
37
+
38
+ export default action
@@ -0,0 +1,28 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Payload {
4
+ /**
5
+ * The unique name of the custom event created in GA4. GA4 does not accept spaces in event names so Segment will replace any spaces with underscores. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).
6
+ */
7
+ name: string
8
+ /**
9
+ * If true, the event name will be converted to lowercase before sending to Google. Event names are case sensitive in GA4 so enable this setting to avoid distinct events for casing differences. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).
10
+ */
11
+ lowercase?: boolean
12
+ /**
13
+ * A unique identifier for a user. See Google's [User-ID for cross-platform analysis](https://support.google.com/analytics/answer/9213390) and [Reporting: deduplicate user counts](https://support.google.com/analytics/answer/9355949?hl=en) documentation for more information on this identifier.
14
+ */
15
+ user_id?: string
16
+ /**
17
+ * The user properties to send to Google Analytics 4. You must create user-scoped dimensions to ensure custom properties are picked up by Google. See Google’s [Custom user properties](https://support.google.com/analytics/answer/9269570) to learn how to set and register user properties.
18
+ */
19
+ user_properties?: {
20
+ [k: string]: unknown
21
+ }
22
+ /**
23
+ * The event parameters to send to Google Analytics 4.
24
+ */
25
+ params?: {
26
+ [k: string]: unknown
27
+ }
28
+ }
@@ -0,0 +1,52 @@
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 { user_id, user_properties, params } from '../ga4-properties'
5
+ import { updateUser } from '../ga4-functions'
6
+
7
+ const normalizeEventName = (name: string, lowercase: boolean | undefined): string => {
8
+ name = name.trim()
9
+ name = name.replace(/\s/g, '_')
10
+
11
+ if (lowercase) {
12
+ name = name.toLowerCase()
13
+ }
14
+ return name
15
+ }
16
+
17
+ const action: BrowserActionDefinition<Settings, Function, Payload> = {
18
+ title: 'Custom Event',
19
+ description: 'Send any custom event',
20
+ defaultSubscription: 'type = "track"',
21
+ platform: 'web',
22
+ fields: {
23
+ name: {
24
+ label: 'Event Name',
25
+ description:
26
+ 'The unique name of the custom event created in GA4. GA4 does not accept spaces in event names so Segment will replace any spaces with underscores. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).',
27
+ type: 'string',
28
+ required: true,
29
+ default: {
30
+ '@path': '$.event'
31
+ }
32
+ },
33
+ lowercase: {
34
+ label: 'Lowercase Event Name',
35
+ description:
36
+ 'If true, the event name will be converted to lowercase before sending to Google. Event names are case sensitive in GA4 so enable this setting to avoid distinct events for casing differences. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).',
37
+ type: 'boolean',
38
+ default: false
39
+ },
40
+ user_id: { ...user_id },
41
+ user_properties: user_properties,
42
+ params: params
43
+ },
44
+ perform: (gtag, { payload }) => {
45
+ updateUser(payload.user_id, payload.user_properties, gtag)
46
+ const event_name = normalizeEventName(payload.name, payload.lowercase)
47
+
48
+ gtag('event', event_name, payload.params)
49
+ }
50
+ }
51
+
52
+ export default action
@@ -0,0 +1,8 @@
1
+ export function updateUser(userID: string | undefined, userProps: object | undefined, gtag: Function): void {
2
+ if (userID) {
3
+ gtag('set', { user_id: userID })
4
+ }
5
+ if (userProps) {
6
+ gtag('set', { user_properties: userProps })
7
+ }
8
+ }