@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,103 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'purchase',
9
+ name: 'Purchase',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ currency: {
14
+ '@path': '$.properties.currency'
15
+ },
16
+ value: {
17
+ '@path': '$.properties.value'
18
+ },
19
+ coupon: {
20
+ '@path': '$.properties.coupon'
21
+ },
22
+ transaction_id: {
23
+ '@path': '$.properties.transaction_id'
24
+ },
25
+ items: [
26
+ {
27
+ item_name: {
28
+ '@path': `$.properties.products.0.name`
29
+ },
30
+ item_id: {
31
+ '@path': `$.properties.products.0.product_id`
32
+ },
33
+ currency: {
34
+ '@path': `$.properties.products.0.currency`
35
+ },
36
+ price: {
37
+ '@path': `$.properties.products.0.price`
38
+ },
39
+ quantity: {
40
+ '@path': `$.properties.products.0.quantity`
41
+ }
42
+ }
43
+ ]
44
+ }
45
+ }
46
+ ]
47
+
48
+ describe('GoogleAnalytics4Web.purchase', () => {
49
+ const settings = {
50
+ measurementID: 'test123'
51
+ }
52
+
53
+ let mockGA4: GA
54
+ let purchaseEvent: any
55
+ beforeEach(async () => {
56
+ jest.restoreAllMocks()
57
+
58
+ const [trackEventPlugin] = await googleAnalytics4Web({
59
+ ...settings,
60
+ subscriptions
61
+ })
62
+ purchaseEvent = trackEventPlugin
63
+
64
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
65
+ mockGA4 = {
66
+ gtag: jest.fn()
67
+ }
68
+ return Promise.resolve(mockGA4.gtag)
69
+ })
70
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
71
+ })
72
+
73
+ test('GA4 purchase Event', async () => {
74
+ const context = new Context({
75
+ event: 'Purchase',
76
+ type: 'track',
77
+ properties: {
78
+ currency: 'USD',
79
+ value: 10,
80
+ transaction_id: 12321,
81
+ products: [
82
+ {
83
+ product_id: '12345',
84
+ name: 'Monopoly: 3rd Edition',
85
+ currency: 'USD'
86
+ }
87
+ ]
88
+ }
89
+ })
90
+ await purchaseEvent.track?.(context)
91
+
92
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
93
+ expect.anything(),
94
+ expect.stringContaining('purchase'),
95
+ expect.objectContaining({
96
+ currency: 'USD',
97
+ transaction_id: 12321,
98
+ items: [{ currency: 'USD', item_id: '12345', item_name: 'Monopoly: 3rd Edition' }],
99
+ value: 10
100
+ })
101
+ )
102
+ })
103
+ })
@@ -0,0 +1,103 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'refund',
9
+ name: 'refund',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ currency: {
14
+ '@path': '$.properties.currency'
15
+ },
16
+ value: {
17
+ '@path': '$.properties.value'
18
+ },
19
+ coupon: {
20
+ '@path': '$.properties.coupon'
21
+ },
22
+ transaction_id: {
23
+ '@path': '$.properties.transaction_id'
24
+ },
25
+ items: [
26
+ {
27
+ item_name: {
28
+ '@path': `$.properties.products.0.name`
29
+ },
30
+ item_id: {
31
+ '@path': `$.properties.products.0.product_id`
32
+ },
33
+ currency: {
34
+ '@path': `$.properties.products.0.currency`
35
+ },
36
+ price: {
37
+ '@path': `$.properties.products.0.price`
38
+ },
39
+ quantity: {
40
+ '@path': `$.properties.products.0.quantity`
41
+ }
42
+ }
43
+ ]
44
+ }
45
+ }
46
+ ]
47
+
48
+ describe('GoogleAnalytics4Web.refund', () => {
49
+ const settings = {
50
+ measurementID: 'test123'
51
+ }
52
+
53
+ let mockGA4: GA
54
+ let refundEvent: any
55
+ beforeEach(async () => {
56
+ jest.restoreAllMocks()
57
+
58
+ const [trackEventPlugin] = await googleAnalytics4Web({
59
+ ...settings,
60
+ subscriptions
61
+ })
62
+ refundEvent = trackEventPlugin
63
+
64
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
65
+ mockGA4 = {
66
+ gtag: jest.fn()
67
+ }
68
+ return Promise.resolve(mockGA4.gtag)
69
+ })
70
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
71
+ })
72
+
73
+ test('GA4 Refund Event', async () => {
74
+ const context = new Context({
75
+ event: 'Refund',
76
+ type: 'track',
77
+ properties: {
78
+ currency: 'USD',
79
+ value: 10,
80
+ transaction_id: 12321,
81
+ products: [
82
+ {
83
+ product_id: '12345',
84
+ name: 'Monopoly: 3rd Edition',
85
+ currency: 'USD'
86
+ }
87
+ ]
88
+ }
89
+ })
90
+ await refundEvent.track?.(context)
91
+
92
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
93
+ expect.anything(),
94
+ expect.stringContaining('refund'),
95
+ expect.objectContaining({
96
+ currency: 'USD',
97
+ transaction_id: 12321,
98
+ items: [{ currency: 'USD', item_id: '12345', item_name: 'Monopoly: 3rd Edition' }],
99
+ value: 10
100
+ })
101
+ )
102
+ })
103
+ })
@@ -0,0 +1,99 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'removeFromCart',
9
+ name: 'Remove from cart',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ currency: {
14
+ '@path': '$.properties.currency'
15
+ },
16
+ value: {
17
+ '@path': '$.properties.value'
18
+ },
19
+ coupon: {
20
+ '@path': '$.properties.coupon'
21
+ },
22
+ items: [
23
+ {
24
+ item_name: {
25
+ '@path': `$.properties.products.0.name`
26
+ },
27
+ item_id: {
28
+ '@path': `$.properties.products.0.product_id`
29
+ },
30
+ currency: {
31
+ '@path': `$.properties.products.0.currency`
32
+ },
33
+ price: {
34
+ '@path': `$.properties.products.0.price`
35
+ },
36
+ quantity: {
37
+ '@path': `$.properties.products.0.quantity`
38
+ }
39
+ }
40
+ ]
41
+ }
42
+ }
43
+ ]
44
+
45
+ describe('GoogleAnalytics4Web.removeFromCart', () => {
46
+ const settings = {
47
+ measurementID: 'test123'
48
+ }
49
+
50
+ let mockGA4: GA
51
+ let removeFromCartEvent: any
52
+ beforeEach(async () => {
53
+ jest.restoreAllMocks()
54
+
55
+ const [trackEventPlugin] = await googleAnalytics4Web({
56
+ ...settings,
57
+ subscriptions
58
+ })
59
+ removeFromCartEvent = trackEventPlugin
60
+
61
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
62
+ mockGA4 = {
63
+ gtag: jest.fn()
64
+ }
65
+ return Promise.resolve(mockGA4.gtag)
66
+ })
67
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
68
+ })
69
+
70
+ test('GA4 removeFromCart Event', async () => {
71
+ const context = new Context({
72
+ event: 'Remove from Cart',
73
+ type: 'track',
74
+ properties: {
75
+ currency: 'USD',
76
+ value: 10,
77
+ products: [
78
+ {
79
+ product_id: '12345',
80
+ name: 'Monopoly: 3rd Edition',
81
+ currency: 'USD'
82
+ }
83
+ ]
84
+ }
85
+ })
86
+
87
+ await removeFromCartEvent.track?.(context)
88
+
89
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
90
+ expect.anything(),
91
+ expect.stringContaining('remove_from_cart'),
92
+ expect.objectContaining({
93
+ currency: 'USD',
94
+ items: [{ currency: 'USD', item_id: '12345', item_name: 'Monopoly: 3rd Edition' }],
95
+ value: 10
96
+ })
97
+ )
98
+ })
99
+ })
@@ -0,0 +1,64 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'search',
9
+ name: 'search',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ search_term: {
14
+ '@path': '$.properties.search_term'
15
+ }
16
+ }
17
+ }
18
+ ]
19
+
20
+ describe('GoogleAnalytics4Web.search', () => {
21
+ const settings = {
22
+ measurementID: 'test123'
23
+ }
24
+
25
+ let mockGA4: GA
26
+ let searchEvent: any
27
+ beforeEach(async () => {
28
+ jest.restoreAllMocks()
29
+
30
+ const [trackEventPlugin] = await googleAnalytics4Web({
31
+ ...settings,
32
+ subscriptions
33
+ })
34
+ searchEvent = trackEventPlugin
35
+
36
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
37
+ mockGA4 = {
38
+ gtag: jest.fn()
39
+ }
40
+ return Promise.resolve(mockGA4.gtag)
41
+ })
42
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
43
+ })
44
+
45
+ test('GA4 search Event', async () => {
46
+ const context = new Context({
47
+ event: 'search',
48
+ type: 'track',
49
+ properties: {
50
+ search_term: 'Monopoly: 3rd Edition'
51
+ }
52
+ })
53
+
54
+ await searchEvent.track?.(context)
55
+
56
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
57
+ expect.anything(),
58
+ expect.stringContaining('search'),
59
+ expect.objectContaining({
60
+ search_term: 'Monopoly: 3rd Edition'
61
+ })
62
+ )
63
+ })
64
+ })
@@ -0,0 +1,96 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'selectItem',
9
+ name: 'Select Item',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ item_list_id: {
14
+ '@path': '$.properties.item_list_id'
15
+ },
16
+ item_list_name: {
17
+ '@path': '$.properties.item_list_name'
18
+ },
19
+ items: [
20
+ {
21
+ item_name: {
22
+ '@path': `$.properties.products.0.name`
23
+ },
24
+ item_id: {
25
+ '@path': `$.properties.products.0.product_id`
26
+ },
27
+ currency: {
28
+ '@path': `$.properties.products.0.currency`
29
+ },
30
+ price: {
31
+ '@path': `$.properties.products.0.price`
32
+ },
33
+ quantity: {
34
+ '@path': `$.properties.products.0.quantity`
35
+ }
36
+ }
37
+ ]
38
+ }
39
+ }
40
+ ]
41
+
42
+ describe('GoogleAnalytics4Web.selectItem', () => {
43
+ const settings = {
44
+ measurementID: 'test123'
45
+ }
46
+
47
+ let mockGA4: GA
48
+ let selectItemEvent: any
49
+ beforeEach(async () => {
50
+ jest.restoreAllMocks()
51
+
52
+ const [trackEventPlugin] = await googleAnalytics4Web({
53
+ ...settings,
54
+ subscriptions
55
+ })
56
+ selectItemEvent = trackEventPlugin
57
+
58
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
59
+ mockGA4 = {
60
+ gtag: jest.fn()
61
+ }
62
+ return Promise.resolve(mockGA4.gtag)
63
+ })
64
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
65
+ })
66
+
67
+ test('GA4 selectItem Event', async () => {
68
+ const context = new Context({
69
+ event: 'Select Item',
70
+ type: 'track',
71
+ properties: {
72
+ item_list_id: 12321,
73
+ item_list_name: 'Monopoly: 3rd Edition',
74
+ products: [
75
+ {
76
+ product_id: '12345',
77
+ name: 'Monopoly: 3rd Edition',
78
+ currency: 'USD'
79
+ }
80
+ ]
81
+ }
82
+ })
83
+
84
+ await selectItemEvent.track?.(context)
85
+
86
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
87
+ expect.anything(),
88
+ expect.stringContaining('select_item'),
89
+ expect.objectContaining({
90
+ item_list_id: 12321,
91
+ item_list_name: 'Monopoly: 3rd Edition',
92
+ items: [{ currency: 'USD', item_id: '12345', item_name: 'Monopoly: 3rd Edition' }]
93
+ })
94
+ )
95
+ })
96
+ })
@@ -0,0 +1,111 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'selectPromotion',
9
+ name: 'Select Promotion',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ creative_name: {
14
+ '@path': '$.properties.creative_name'
15
+ },
16
+ creative_slot: {
17
+ '@path': '$.properties.creative_slot'
18
+ },
19
+ location_id: {
20
+ '@path': '$.properties.location_id'
21
+ },
22
+ promotion_id: {
23
+ '@path': '$.properties.promotion_id'
24
+ },
25
+ promotion_name: {
26
+ '@path': '$.properties.promotion_name'
27
+ },
28
+ items: [
29
+ {
30
+ item_name: {
31
+ '@path': `$.properties.products.0.name`
32
+ },
33
+ item_id: {
34
+ '@path': `$.properties.products.0.product_id`
35
+ },
36
+ currency: {
37
+ '@path': `$.properties.products.0.currency`
38
+ },
39
+ price: {
40
+ '@path': `$.properties.products.0.price`
41
+ },
42
+ quantity: {
43
+ '@path': `$.properties.products.0.quantity`
44
+ }
45
+ }
46
+ ]
47
+ }
48
+ }
49
+ ]
50
+
51
+ describe('GoogleAnalytics4Web.selectPromotion', () => {
52
+ const settings = {
53
+ measurementID: 'test123'
54
+ }
55
+
56
+ let mockGA4: GA
57
+ let selectPromotionEvent: any
58
+ beforeEach(async () => {
59
+ jest.restoreAllMocks()
60
+
61
+ const [trackEventPlugin] = await googleAnalytics4Web({
62
+ ...settings,
63
+ subscriptions
64
+ })
65
+ selectPromotionEvent = trackEventPlugin
66
+
67
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
68
+ mockGA4 = {
69
+ gtag: jest.fn()
70
+ }
71
+ return Promise.resolve(mockGA4.gtag)
72
+ })
73
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
74
+ })
75
+
76
+ test('GA4 selectPromotion Event', async () => {
77
+ const context = new Context({
78
+ event: 'Select Promotion',
79
+ type: 'track',
80
+ properties: {
81
+ creative_name: 'summer_banner2',
82
+ creative_slot: 'featured_app_1',
83
+ location_id: 'ChIJIQBpAG2ahYAR_6128GcTUEo',
84
+ promotion_id: 'P_12345',
85
+ promotion_name: 'Summer Sale',
86
+ products: [
87
+ {
88
+ product_id: '12345',
89
+ name: 'Monopoly: 3rd Edition',
90
+ currency: 'USD'
91
+ }
92
+ ]
93
+ }
94
+ })
95
+
96
+ await selectPromotionEvent.track?.(context)
97
+
98
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
99
+ expect.anything(),
100
+ expect.stringContaining('select_promotion'),
101
+ expect.objectContaining({
102
+ creative_name: 'summer_banner2',
103
+ creative_slot: 'featured_app_1',
104
+ location_id: 'ChIJIQBpAG2ahYAR_6128GcTUEo',
105
+ promotion_id: 'P_12345',
106
+ promotion_name: 'Summer Sale',
107
+ items: [{ currency: 'USD', item_id: '12345', item_name: 'Monopoly: 3rd Edition' }]
108
+ })
109
+ )
110
+ })
111
+ })
@@ -0,0 +1,62 @@
1
+ import { Subscription } from '@segment/browser-destination-runtime/types'
2
+ import { Analytics, Context } from '@segment/analytics-next'
3
+ import googleAnalytics4Web, { destination } from '../index'
4
+ import { GA } from '../types'
5
+
6
+ const subscriptions: Subscription[] = [
7
+ {
8
+ partnerAction: 'signUp',
9
+ name: 'signUp',
10
+ enabled: true,
11
+ subscribe: 'type = "track"',
12
+ mapping: {
13
+ method: {
14
+ '@path': '$.properties.method'
15
+ }
16
+ }
17
+ }
18
+ ]
19
+
20
+ describe('GoogleAnalytics4Web.signUp', () => {
21
+ const settings = {
22
+ measurementID: 'test123'
23
+ }
24
+
25
+ let mockGA4: GA
26
+ let signUpEvent: any
27
+ beforeEach(async () => {
28
+ jest.restoreAllMocks()
29
+
30
+ const [trackEventPlugin] = await googleAnalytics4Web({
31
+ ...settings,
32
+ subscriptions
33
+ })
34
+ signUpEvent = trackEventPlugin
35
+
36
+ jest.spyOn(destination, 'initialize').mockImplementation(() => {
37
+ mockGA4 = {
38
+ gtag: jest.fn()
39
+ }
40
+ return Promise.resolve(mockGA4.gtag)
41
+ })
42
+ await trackEventPlugin.load(Context.system(), {} as Analytics)
43
+ })
44
+
45
+ test('GA4 signUp Event', async () => {
46
+ const context = new Context({
47
+ event: 'signUp',
48
+ type: 'track',
49
+ properties: {
50
+ method: 'Google'
51
+ }
52
+ })
53
+
54
+ await signUpEvent.track?.(context)
55
+
56
+ expect(mockGA4.gtag).toHaveBeenCalledWith(
57
+ expect.anything(),
58
+ expect.stringContaining('sign_up'),
59
+ expect.objectContaining({ method: 'Google' })
60
+ )
61
+ })
62
+ })