@graphcommerce/googleanalytics 8.0.4-canary.0 → 8.0.4
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/CHANGELOG.md +14 -0
- package/README.md +4 -0
- package/components/index.ts +0 -1
- package/package.json +14 -11
- package/components/GoogleAnalyticsItemList.tsx +0 -63
- package/events/gtagAddPaymentInfo/GtagAddPaymentInfo.graphql +0 -36
- package/events/gtagAddPaymentInfo/gtagAddPaymentInfo.ts +0 -22
- package/events/gtagAddPurchaseInfo/gtagAddPurchaseInfo.ts +0 -27
- package/events/gtagAddShippingInfo/GtagAddShippingInfo.graphql +0 -38
- package/events/gtagAddShippingInfo/gtagAddShippingInfo.ts +0 -23
- package/events/gtagAddToCart/GtagAddToCart.graphql +0 -20
- package/events/gtagAddToCart/gtagAddToCart.ts +0 -34
- package/events/gtagBeginCheckout/GtagBeginCheckout.graphql +0 -38
- package/events/gtagBeginCheckout/gtagBeginCheckout.ts +0 -21
- package/events/gtagRemoveFromCart/GtagRemoveFromCart.graphql +0 -36
- package/events/gtagRemoveFromCart/gtagRemoveFromCart.ts +0 -22
- package/events/gtagViewCart/GtagViewCart.graphql +0 -35
- package/events/gtagViewCart/gtagViewCart.ts +0 -22
- package/events/productToGtagItem/ProductToGtagItem.graphql +0 -20
- package/events/productToGtagItem/productToGtagItem.ts +0 -38
- package/index.ts +0 -6
- package/plugins/GaAddProductsToCartForm.tsx +0 -24
- package/plugins/GaCartStartCheckout.tsx +0 -22
- package/plugins/GaCartStartCheckoutLinkOrButton.tsx +0 -36
- package/plugins/GaPaymentMethodButton.tsx +0 -27
- package/plugins/GaPaymentMethodContextProvider.tsx +0 -21
- package/plugins/GaProductListItem.tsx +0 -25
- package/plugins/GaProductListItemsBase.tsx +0 -18
- package/plugins/GaRemoveItemFromCart.tsx +0 -29
- package/plugins/GaRemoveItemFromCartFab.tsx +0 -29
- package/plugins/GaShippingMethodForm.tsx +0 -23
- package/plugins/GaUpdateItemQuantity.tsx +0 -61
- package/plugins/GaViewItem.tsx +0 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 8.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2158](https://github.com/graphcommerce-org/graphcommerce/pull/2158) [`34de808`](https://github.com/graphcommerce-org/graphcommerce/commit/34de8085e9352d1f3b20b26746685370ea10ab90) - Extracted the datalayer from the googleanalytics package and moved to google-datalayer package. Make sure Google Analytics and Google Tagmanager both can send events individually. Be able to configure the datalayer will send as GA4 or legacy GA3 events.
|
|
8
|
+
([@mikekeehnen](https://github.com/mikekeehnen))
|
|
9
|
+
|
|
10
|
+
## 8.0.4-canary.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- [#2158](https://github.com/graphcommerce-org/graphcommerce/pull/2158) [`34de808`](https://github.com/graphcommerce-org/graphcommerce/commit/34de8085e9352d1f3b20b26746685370ea10ab90) - Extracted the datalayer from the googleanalytics package and moved to google-datalayer package. Make sure Google Analytics and Google Tagmanager both can send events individually. Be able to configure the datalayer will send as GA4 or legacy GA3 events.
|
|
15
|
+
([@mikekeehnen](https://github.com/mikekeehnen))
|
|
16
|
+
|
|
3
17
|
## 8.0.4-canary.0
|
|
4
18
|
|
|
5
19
|
## 8.0.3
|
package/README.md
CHANGED
|
@@ -20,3 +20,7 @@ Besides the GA4 integration it also tracks the following events:
|
|
|
20
20
|
|
|
21
21
|
Configure the following ([configuration values](./Config.graphqls)) in your
|
|
22
22
|
graphcommerce.config.js
|
|
23
|
+
|
|
24
|
+
Make sure you also configure the 'Page changes based on browser history events.'
|
|
25
|
+
configuration in Google Analytics, see
|
|
26
|
+
[docs](https://developers.google.com/analytics/devguides/collection/ga4/single-page-applications?implementation=browser-history#implement_single-page_application_measurement).
|
package/components/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/googleanalytics",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "8.0.4
|
|
5
|
+
"version": "8.0.4",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -14,17 +14,20 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/gtag.js": "^0.0.18"
|
|
16
16
|
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@graphcommerce/google-datalayer": "8.0.4"
|
|
19
|
+
},
|
|
17
20
|
"peerDependencies": {
|
|
18
|
-
"@graphcommerce/eslint-config-pwa": "^8.0.4
|
|
19
|
-
"@graphcommerce/graphql-mesh": "^8.0.4
|
|
20
|
-
"@graphcommerce/magento-cart": "^8.0.4
|
|
21
|
-
"@graphcommerce/magento-cart-payment-method": "^8.0.4
|
|
22
|
-
"@graphcommerce/magento-cart-shipping-method": "^8.0.4
|
|
23
|
-
"@graphcommerce/magento-product": "^8.0.4
|
|
24
|
-
"@graphcommerce/next-config": "^8.0.4
|
|
25
|
-
"@graphcommerce/next-ui": "^8.0.4
|
|
26
|
-
"@graphcommerce/prettier-config-pwa": "^8.0.4
|
|
27
|
-
"@graphcommerce/typescript-config-pwa": "^8.0.4
|
|
21
|
+
"@graphcommerce/eslint-config-pwa": "^8.0.4",
|
|
22
|
+
"@graphcommerce/graphql-mesh": "^8.0.4",
|
|
23
|
+
"@graphcommerce/magento-cart": "^8.0.4",
|
|
24
|
+
"@graphcommerce/magento-cart-payment-method": "^8.0.4",
|
|
25
|
+
"@graphcommerce/magento-cart-shipping-method": "^8.0.4",
|
|
26
|
+
"@graphcommerce/magento-product": "^8.0.4",
|
|
27
|
+
"@graphcommerce/next-config": "^8.0.4",
|
|
28
|
+
"@graphcommerce/next-ui": "^8.0.4",
|
|
29
|
+
"@graphcommerce/prettier-config-pwa": "^8.0.4",
|
|
30
|
+
"@graphcommerce/typescript-config-pwa": "^8.0.4",
|
|
28
31
|
"@mui/material": "^5.10.16",
|
|
29
32
|
"next": "*",
|
|
30
33
|
"react": "^18.2.0",
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { nonNullable, useMemoObject } from '@graphcommerce/next-ui'
|
|
2
|
-
import { useEventCallback } from '@mui/material'
|
|
3
|
-
import React, { useContext, useEffect, useMemo } from 'react'
|
|
4
|
-
import { ProductToGtagItemFragment } from '../events/productToGtagItem/ProductToGtagItem.gql'
|
|
5
|
-
import { GtagItem, productToGtagItem } from '../events/productToGtagItem/productToGtagItem'
|
|
6
|
-
|
|
7
|
-
export type UseGtagViewItemListProps<
|
|
8
|
-
P extends ProductToGtagItemFragment = ProductToGtagItemFragment,
|
|
9
|
-
> = {
|
|
10
|
-
title: string
|
|
11
|
-
items?: (P | null | undefined)[] | null
|
|
12
|
-
listId?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type ViewItemList = {
|
|
16
|
-
item_list_id: string
|
|
17
|
-
item_list_name: string
|
|
18
|
-
items: GtagItem[]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const GoogleAnalyticsItemListContext = React.createContext<{
|
|
22
|
-
item_list_id: string
|
|
23
|
-
item_list_name: string
|
|
24
|
-
}>({ item_list_id: '', item_list_name: '' })
|
|
25
|
-
|
|
26
|
-
export function useGoogleAnalyticsListItemHandler(item: ProductToGtagItemFragment) {
|
|
27
|
-
const { item_list_id, item_list_name } = useContext(GoogleAnalyticsItemListContext)
|
|
28
|
-
return useEventCallback(() =>
|
|
29
|
-
globalThis.gtag?.('event', 'select_item', {
|
|
30
|
-
item_list_id,
|
|
31
|
-
item_list_name,
|
|
32
|
-
items: productToGtagItem(item),
|
|
33
|
-
}),
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function GoogleAnalyticsItemList<
|
|
38
|
-
P extends ProductToGtagItemFragment = ProductToGtagItemFragment,
|
|
39
|
-
>(props: UseGtagViewItemListProps<P> & { children: React.ReactNode }) {
|
|
40
|
-
const { title, items, listId, children } = props
|
|
41
|
-
|
|
42
|
-
const eventData: ViewItemList = useMemoObject({
|
|
43
|
-
item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
|
|
44
|
-
item_list_name: title,
|
|
45
|
-
items: items?.map((item) => (item ? productToGtagItem(item) : null)).filter(nonNullable) ?? [],
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
useEffect(() => globalThis.gtag?.('event', 'view_item_list', eventData), [eventData])
|
|
49
|
-
|
|
50
|
-
const value = useMemo(
|
|
51
|
-
() => ({
|
|
52
|
-
item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
|
|
53
|
-
item_list_name: title ?? listId,
|
|
54
|
-
}),
|
|
55
|
-
[listId, title],
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
return (
|
|
59
|
-
<GoogleAnalyticsItemListContext.Provider value={value}>
|
|
60
|
-
{children}
|
|
61
|
-
</GoogleAnalyticsItemListContext.Provider>
|
|
62
|
-
)
|
|
63
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
fragment GtagAddPaymentInfo on Cart
|
|
2
|
-
@inject(into: ["PaymentMethodContext", "PaymentMethodUpdated"]) {
|
|
3
|
-
items {
|
|
4
|
-
uid
|
|
5
|
-
prices {
|
|
6
|
-
price {
|
|
7
|
-
currency
|
|
8
|
-
value
|
|
9
|
-
}
|
|
10
|
-
discounts {
|
|
11
|
-
amount {
|
|
12
|
-
currency
|
|
13
|
-
value
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
quantity
|
|
18
|
-
product {
|
|
19
|
-
uid
|
|
20
|
-
name
|
|
21
|
-
sku
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
applied_coupons {
|
|
25
|
-
code
|
|
26
|
-
}
|
|
27
|
-
prices {
|
|
28
|
-
grand_total {
|
|
29
|
-
currency
|
|
30
|
-
value
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
selected_payment_method {
|
|
34
|
-
code
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { GtagAddPaymentInfoFragment } from './GtagAddPaymentInfo.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagAddPaymentInfo<C extends GtagAddPaymentInfoFragment>(cart?: C | null) {
|
|
4
|
-
globalThis.gtag?.('event', 'add_payment_info', {
|
|
5
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
6
|
-
value: cart?.prices?.grand_total?.value,
|
|
7
|
-
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
|
|
8
|
-
payment_type: cart?.selected_payment_method?.code,
|
|
9
|
-
items: cart?.items?.map((item) => ({
|
|
10
|
-
item_id: item?.product.sku,
|
|
11
|
-
item_name: item?.product.name,
|
|
12
|
-
currency: item?.prices?.price.currency,
|
|
13
|
-
discount: item?.prices?.discounts?.reduce(
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
15
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
16
|
-
0,
|
|
17
|
-
),
|
|
18
|
-
price: item?.prices?.price.value,
|
|
19
|
-
quantity: item?.quantity,
|
|
20
|
-
})),
|
|
21
|
-
})
|
|
22
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { PaymentMethodContextFragment } from '@graphcommerce/magento-cart-payment-method/Api/PaymentMethodContext.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagAddPurchaseInfo(
|
|
4
|
-
orderNumber: string,
|
|
5
|
-
cart: PaymentMethodContextFragment | null | undefined,
|
|
6
|
-
) {
|
|
7
|
-
globalThis.gtag?.('event', 'purchase', {
|
|
8
|
-
transaction_id: orderNumber,
|
|
9
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
10
|
-
value: cart?.prices?.grand_total?.value,
|
|
11
|
-
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
|
|
12
|
-
payment_type: cart?.selected_payment_method?.code,
|
|
13
|
-
tax: cart?.prices?.applied_taxes?.reduce((sum, tax) => sum + (tax?.amount?.value ?? 0), 0),
|
|
14
|
-
items: cart?.items?.map((item) => ({
|
|
15
|
-
item_id: item?.product.sku,
|
|
16
|
-
item_name: item?.product.name,
|
|
17
|
-
currency: item?.prices?.price.currency,
|
|
18
|
-
discount: item?.prices?.discounts?.reduce(
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
20
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
21
|
-
0,
|
|
22
|
-
),
|
|
23
|
-
price: item?.prices?.price.value,
|
|
24
|
-
quantity: item?.quantity,
|
|
25
|
-
})),
|
|
26
|
-
})
|
|
27
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
fragment GtagAddShippingInfo on Cart @inject(into: ["ShippingMethodSelected"]) {
|
|
2
|
-
prices {
|
|
3
|
-
grand_total {
|
|
4
|
-
currency
|
|
5
|
-
value
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
applied_coupons {
|
|
9
|
-
code
|
|
10
|
-
}
|
|
11
|
-
items {
|
|
12
|
-
__typename
|
|
13
|
-
uid
|
|
14
|
-
product {
|
|
15
|
-
uid
|
|
16
|
-
sku
|
|
17
|
-
name
|
|
18
|
-
}
|
|
19
|
-
prices {
|
|
20
|
-
price {
|
|
21
|
-
value
|
|
22
|
-
currency
|
|
23
|
-
}
|
|
24
|
-
discounts {
|
|
25
|
-
amount {
|
|
26
|
-
value
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
quantity
|
|
31
|
-
... on ConfigurableCartItem {
|
|
32
|
-
configured_variant {
|
|
33
|
-
uid
|
|
34
|
-
sku
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { GtagAddShippingInfoFragment } from './GtagAddShippingInfo.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagAddShippingInfo<C extends GtagAddShippingInfoFragment>(cart?: C | null) {
|
|
4
|
-
if (!cart) return
|
|
5
|
-
|
|
6
|
-
globalThis.gtag?.('event', 'add_shipping_info', {
|
|
7
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
8
|
-
value: cart?.prices?.grand_total?.value,
|
|
9
|
-
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
|
|
10
|
-
items: cart?.items?.map((item) => ({
|
|
11
|
-
item_id: item?.product.sku,
|
|
12
|
-
item_name: item?.product.name,
|
|
13
|
-
currency: item?.prices?.price.currency,
|
|
14
|
-
discount: item?.prices?.discounts?.reduce(
|
|
15
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
16
|
-
0,
|
|
17
|
-
),
|
|
18
|
-
item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
|
|
19
|
-
price: item?.prices?.price.value,
|
|
20
|
-
quantity: item?.quantity,
|
|
21
|
-
})),
|
|
22
|
-
})
|
|
23
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
fragment GtagAddToCart on Cart @inject(into: ["CartItemCountChanged"]) {
|
|
2
|
-
items {
|
|
3
|
-
prices {
|
|
4
|
-
discounts {
|
|
5
|
-
amount {
|
|
6
|
-
currency
|
|
7
|
-
value
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
row_total_including_tax {
|
|
11
|
-
currency
|
|
12
|
-
value
|
|
13
|
-
}
|
|
14
|
-
price {
|
|
15
|
-
currency
|
|
16
|
-
value
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { FetchResult } from '@graphcommerce/graphql'
|
|
2
|
-
import { AddProductsToCartMutation, AddProductsToCartFields } from '@graphcommerce/magento-product'
|
|
3
|
-
|
|
4
|
-
export const gtagAddToCart = (
|
|
5
|
-
result: FetchResult<AddProductsToCartMutation>,
|
|
6
|
-
variables: AddProductsToCartFields,
|
|
7
|
-
) => {
|
|
8
|
-
const addedItem = result.data?.addProductsToCart?.cart.items?.slice(-1)[0]
|
|
9
|
-
|
|
10
|
-
if (addedItem && addedItem.prices && addedItem.prices.row_total_including_tax.value) {
|
|
11
|
-
// we need to manually calculate pricePerItemInclTax (https://github.com/magento/magento2/issues/33848)
|
|
12
|
-
const pricePerItemInclTax = addedItem.prices.row_total_including_tax.value / addedItem.quantity
|
|
13
|
-
const addToCartValue = pricePerItemInclTax * variables.cartItems[0].quantity
|
|
14
|
-
|
|
15
|
-
globalThis.gtag?.('event', 'add_to_cart', {
|
|
16
|
-
currency: addedItem?.prices?.price.currency,
|
|
17
|
-
value: addToCartValue,
|
|
18
|
-
items: [
|
|
19
|
-
{
|
|
20
|
-
item_id: addedItem?.product.sku,
|
|
21
|
-
item_name: addedItem?.product.name,
|
|
22
|
-
currency: addedItem?.prices?.price.currency,
|
|
23
|
-
price: pricePerItemInclTax,
|
|
24
|
-
quantity: variables.cartItems[0].quantity,
|
|
25
|
-
discount: addedItem?.prices?.discounts?.reduce(
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
27
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
28
|
-
0,
|
|
29
|
-
),
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
fragment GtagBeginCheckout on Cart @inject(into: ["CartStartCheckout"]) {
|
|
2
|
-
prices {
|
|
3
|
-
grand_total {
|
|
4
|
-
currency
|
|
5
|
-
value
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
applied_coupons {
|
|
9
|
-
code
|
|
10
|
-
}
|
|
11
|
-
items {
|
|
12
|
-
__typename
|
|
13
|
-
uid
|
|
14
|
-
product {
|
|
15
|
-
uid
|
|
16
|
-
sku
|
|
17
|
-
name
|
|
18
|
-
}
|
|
19
|
-
prices {
|
|
20
|
-
price {
|
|
21
|
-
value
|
|
22
|
-
currency
|
|
23
|
-
}
|
|
24
|
-
discounts {
|
|
25
|
-
amount {
|
|
26
|
-
value
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
quantity
|
|
31
|
-
... on ConfigurableCartItem {
|
|
32
|
-
configured_variant {
|
|
33
|
-
uid
|
|
34
|
-
sku
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { GtagBeginCheckoutFragment } from './GtagBeginCheckout.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagBeginCheckout<C extends GtagBeginCheckoutFragment>(cart?: C | null) {
|
|
4
|
-
globalThis.gtag?.('event', 'begin_checkout', {
|
|
5
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
6
|
-
value: cart?.prices?.grand_total?.value,
|
|
7
|
-
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
|
|
8
|
-
items: cart?.items?.map((item) => ({
|
|
9
|
-
item_id: item?.product.sku,
|
|
10
|
-
item_name: item?.product.name,
|
|
11
|
-
currency: item?.prices?.price.currency,
|
|
12
|
-
discount: item?.prices?.discounts?.reduce(
|
|
13
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
14
|
-
0,
|
|
15
|
-
),
|
|
16
|
-
item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
|
|
17
|
-
price: item?.prices?.price.value,
|
|
18
|
-
quantity: item?.quantity,
|
|
19
|
-
})),
|
|
20
|
-
})
|
|
21
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
fragment GtagRemoveFromCart on Cart @inject(into: ["CartItemCountChanged"]) {
|
|
2
|
-
__typename
|
|
3
|
-
prices {
|
|
4
|
-
grand_total {
|
|
5
|
-
currency
|
|
6
|
-
value
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
items {
|
|
10
|
-
__typename
|
|
11
|
-
uid
|
|
12
|
-
product {
|
|
13
|
-
uid
|
|
14
|
-
sku
|
|
15
|
-
name
|
|
16
|
-
}
|
|
17
|
-
prices {
|
|
18
|
-
price {
|
|
19
|
-
value
|
|
20
|
-
currency
|
|
21
|
-
}
|
|
22
|
-
discounts {
|
|
23
|
-
amount {
|
|
24
|
-
value
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
quantity
|
|
29
|
-
... on ConfigurableCartItem {
|
|
30
|
-
configured_variant {
|
|
31
|
-
uid
|
|
32
|
-
sku
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { GtagRemoveFromCartFragment } from './GtagRemoveFromCart.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagRemoveFromCart<C extends GtagRemoveFromCartFragment>(cart?: C | null) {
|
|
4
|
-
if (!cart) return
|
|
5
|
-
|
|
6
|
-
globalThis.gtag?.('event', 'remove_from_cart', {
|
|
7
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
8
|
-
value: cart?.prices?.grand_total?.value,
|
|
9
|
-
items: cart?.items?.map((item) => ({
|
|
10
|
-
item_id: item?.product.sku,
|
|
11
|
-
item_name: item?.product.name,
|
|
12
|
-
currency: item?.prices?.price.currency,
|
|
13
|
-
discount: item?.prices?.discounts?.reduce(
|
|
14
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
15
|
-
0,
|
|
16
|
-
),
|
|
17
|
-
item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
|
|
18
|
-
price: item?.prices?.price.value,
|
|
19
|
-
quantity: item?.quantity,
|
|
20
|
-
})),
|
|
21
|
-
})
|
|
22
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
fragment GtagViewCart on Cart @inject(into: ["CartItemCountChanged"]) {
|
|
2
|
-
prices {
|
|
3
|
-
grand_total {
|
|
4
|
-
currency
|
|
5
|
-
value
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
items {
|
|
9
|
-
__typename
|
|
10
|
-
uid
|
|
11
|
-
product {
|
|
12
|
-
uid
|
|
13
|
-
sku
|
|
14
|
-
name
|
|
15
|
-
}
|
|
16
|
-
prices {
|
|
17
|
-
price {
|
|
18
|
-
value
|
|
19
|
-
currency
|
|
20
|
-
}
|
|
21
|
-
discounts {
|
|
22
|
-
amount {
|
|
23
|
-
value
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
quantity
|
|
28
|
-
... on ConfigurableCartItem {
|
|
29
|
-
configured_variant {
|
|
30
|
-
uid
|
|
31
|
-
sku
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { GtagViewCartFragment } from './GtagViewCart.gql'
|
|
2
|
-
|
|
3
|
-
export function gtagViewCart<C extends GtagViewCartFragment>(cart?: C | null) {
|
|
4
|
-
if (!cart) return
|
|
5
|
-
|
|
6
|
-
globalThis.gtag?.('event', 'view_cart', {
|
|
7
|
-
currency: cart?.prices?.grand_total?.currency,
|
|
8
|
-
value: cart?.prices?.grand_total?.value,
|
|
9
|
-
items: cart?.items?.map((item) => ({
|
|
10
|
-
item_id: item?.product.sku,
|
|
11
|
-
item_name: item?.product.name,
|
|
12
|
-
currency: item?.prices?.price.currency,
|
|
13
|
-
discount: item?.prices?.discounts?.reduce(
|
|
14
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
15
|
-
0,
|
|
16
|
-
),
|
|
17
|
-
item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
|
|
18
|
-
price: item?.prices?.price.value,
|
|
19
|
-
quantity: item?.quantity,
|
|
20
|
-
})),
|
|
21
|
-
})
|
|
22
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { ProductToGtagItemFragment } from './ProductToGtagItem.gql'
|
|
2
|
-
|
|
3
|
-
export type GtagItem = {
|
|
4
|
-
item_id: string
|
|
5
|
-
item_name: string
|
|
6
|
-
affiliation?: string
|
|
7
|
-
coupon?: string
|
|
8
|
-
currency?: string
|
|
9
|
-
discount?: number
|
|
10
|
-
index?: number
|
|
11
|
-
item_brand?: string
|
|
12
|
-
item_category?: string
|
|
13
|
-
item_category2?: string
|
|
14
|
-
item_category3?: string
|
|
15
|
-
item_category4?: string
|
|
16
|
-
item_category5?: string
|
|
17
|
-
item_list_id?: string
|
|
18
|
-
item_list_name?: string
|
|
19
|
-
item_variant?: string
|
|
20
|
-
location_id?: string
|
|
21
|
-
price?: number
|
|
22
|
-
quantity?: number
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function productToGtagItem<P extends ProductToGtagItemFragment>(item: P): GtagItem {
|
|
26
|
-
return {
|
|
27
|
-
item_id: item.sku ?? '',
|
|
28
|
-
item_name: item.name ?? '',
|
|
29
|
-
price: item.price_range?.minimum_price.final_price.value ?? undefined,
|
|
30
|
-
currency: item.price_range?.minimum_price.final_price.currency ?? undefined,
|
|
31
|
-
discount: item.price_range?.minimum_price.discount?.amount_off ?? undefined,
|
|
32
|
-
item_category: item.categories?.[0]?.name ?? undefined,
|
|
33
|
-
item_category2: item.categories?.[1]?.name ?? undefined,
|
|
34
|
-
item_category3: item.categories?.[2]?.name ?? undefined,
|
|
35
|
-
item_category4: item.categories?.[3]?.name ?? undefined,
|
|
36
|
-
item_category5: item.categories?.[4]?.name ?? undefined,
|
|
37
|
-
}
|
|
38
|
-
}
|
package/index.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export * from './components'
|
|
2
|
-
export * from './events/gtagBeginCheckout/gtagBeginCheckout'
|
|
3
|
-
export * from './events/gtagAddPaymentInfo/gtagAddPaymentInfo'
|
|
4
|
-
export * from './events/gtagAddPurchaseInfo/gtagAddPurchaseInfo'
|
|
5
|
-
export * from './events/gtagAddShippingInfo/gtagAddShippingInfo'
|
|
6
|
-
export * from './events/gtagAddToCart/gtagAddToCart'
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { AddProductsToCartFormProps } from '@graphcommerce/magento-product'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagAddToCart } from '../events/gtagAddToCart/gtagAddToCart'
|
|
4
|
-
|
|
5
|
-
export const component = 'AddProductsToCartForm'
|
|
6
|
-
export const exported = '@graphcommerce/magento-product'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
/** When a product is added to the Cart, send a Google Analytics event */
|
|
10
|
-
function GaAddProductsToCartForm(props: PluginProps<AddProductsToCartFormProps>) {
|
|
11
|
-
const { Prev, onComplete, ...rest } = props
|
|
12
|
-
|
|
13
|
-
return (
|
|
14
|
-
<Prev
|
|
15
|
-
{...rest}
|
|
16
|
-
onComplete={(data, variables) => {
|
|
17
|
-
gtagAddToCart(data, variables)
|
|
18
|
-
return onComplete?.(data, variables)
|
|
19
|
-
}}
|
|
20
|
-
/>
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const Plugin = GaAddProductsToCartForm
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { CartStartCheckoutProps } from '@graphcommerce/magento-cart'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagBeginCheckout } from '../events/gtagBeginCheckout/gtagBeginCheckout'
|
|
4
|
-
|
|
5
|
-
export const component = 'CartStartCheckout'
|
|
6
|
-
export const exported = '@graphcommerce/magento-cart'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
export function GaCartStartCheckout(props: PluginProps<CartStartCheckoutProps>) {
|
|
10
|
-
const { Prev, onStart, ...rest } = props
|
|
11
|
-
return (
|
|
12
|
-
<Prev
|
|
13
|
-
{...rest}
|
|
14
|
-
onStart={(e, cart) => {
|
|
15
|
-
gtagBeginCheckout(cart)
|
|
16
|
-
return onStart?.(e, cart)
|
|
17
|
-
}}
|
|
18
|
-
/>
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const Plugin = GaCartStartCheckout
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { CartStartCheckoutLinkOrButtonProps } from '@graphcommerce/magento-cart'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { useMemoObject } from '@graphcommerce/next-ui'
|
|
4
|
-
import { useEffect } from 'react'
|
|
5
|
-
import { gtagBeginCheckout } from '../events/gtagBeginCheckout/gtagBeginCheckout'
|
|
6
|
-
import { gtagViewCart } from '../events/gtagViewCart/gtagViewCart'
|
|
7
|
-
|
|
8
|
-
export const component = 'CartStartCheckoutLinkOrButton'
|
|
9
|
-
export const exported = '@graphcommerce/magento-cart'
|
|
10
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
11
|
-
|
|
12
|
-
export function GaCartStartCheckoutLinkOrButton(
|
|
13
|
-
props: PluginProps<CartStartCheckoutLinkOrButtonProps>,
|
|
14
|
-
) {
|
|
15
|
-
const { Prev, onStart, ...rest } = props
|
|
16
|
-
|
|
17
|
-
const cartObject = useMemoObject({ items: rest.items, prices: rest.prices })
|
|
18
|
-
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
if (cartObject.items) {
|
|
21
|
-
gtagViewCart(cartObject)
|
|
22
|
-
}
|
|
23
|
-
}, [cartObject])
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<Prev
|
|
27
|
-
{...rest}
|
|
28
|
-
onStart={(e, cart) => {
|
|
29
|
-
gtagBeginCheckout(cart)
|
|
30
|
-
return onStart?.(e, cart)
|
|
31
|
-
}}
|
|
32
|
-
/>
|
|
33
|
-
)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const Plugin = GaCartStartCheckoutLinkOrButton
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { useCartQuery } from '@graphcommerce/magento-cart'
|
|
2
|
-
import { PaymentMethodButtonProps } from '@graphcommerce/magento-cart-payment-method'
|
|
3
|
-
import { GetPaymentMethodContextDocument } from '@graphcommerce/magento-cart-payment-method/PaymentMethodContext/GetPaymentMethodContext.gql'
|
|
4
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
5
|
-
import { gtagAddPaymentInfo } from '../events/gtagAddPaymentInfo/gtagAddPaymentInfo'
|
|
6
|
-
|
|
7
|
-
export const component = 'PaymentMethodButton'
|
|
8
|
-
export const exported = '@graphcommerce/magento-cart-payment-method'
|
|
9
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
10
|
-
|
|
11
|
-
// @todo This plugin can probably be migrated to the actual form that is submitted.
|
|
12
|
-
function GaPaymentMethodButton(props: PluginProps<PaymentMethodButtonProps>) {
|
|
13
|
-
const { Prev, onSubmitSuccessful, ...rest } = props
|
|
14
|
-
const methodContext = useCartQuery(GetPaymentMethodContextDocument)
|
|
15
|
-
|
|
16
|
-
return (
|
|
17
|
-
<Prev
|
|
18
|
-
{...rest}
|
|
19
|
-
onSubmitSuccessful={() => {
|
|
20
|
-
gtagAddPaymentInfo(methodContext.data?.cart)
|
|
21
|
-
return onSubmitSuccessful?.()
|
|
22
|
-
}}
|
|
23
|
-
/>
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const Plugin = GaPaymentMethodButton
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { PaymentMethodContextProviderProps } from '@graphcommerce/magento-cart-payment-method'
|
|
2
|
-
import type { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagAddPurchaseInfo } from '../events/gtagAddPurchaseInfo/gtagAddPurchaseInfo'
|
|
4
|
-
|
|
5
|
-
export const component = 'PaymentMethodContextProvider'
|
|
6
|
-
export const exported = '@graphcommerce/magento-cart-payment-method'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
function GaPaymentMethodContextProvider(props: PluginProps<PaymentMethodContextProviderProps>) {
|
|
10
|
-
const { Prev, onSuccess, ...rest } = props
|
|
11
|
-
return (
|
|
12
|
-
<Prev
|
|
13
|
-
{...rest}
|
|
14
|
-
onSuccess={(orderNumber, cart) => {
|
|
15
|
-
gtagAddPurchaseInfo(orderNumber, cart)
|
|
16
|
-
return onSuccess?.(orderNumber, cart)
|
|
17
|
-
}}
|
|
18
|
-
/>
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
export const Plugin = GaPaymentMethodContextProvider
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { ProductListItemProps } from '@graphcommerce/magento-product'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { useEventCallback } from '@mui/material'
|
|
4
|
-
import { useGoogleAnalyticsListItemHandler } from '../components/GoogleAnalyticsItemList'
|
|
5
|
-
|
|
6
|
-
export const component = 'ProductListItem'
|
|
7
|
-
export const exported = '@graphcommerce/magento-product'
|
|
8
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
9
|
-
|
|
10
|
-
function GaProductListItemsBase(props: PluginProps<ProductListItemProps>) {
|
|
11
|
-
const { Prev, onClick, ...rest } = props
|
|
12
|
-
const handle = useGoogleAnalyticsListItemHandler(rest)
|
|
13
|
-
|
|
14
|
-
return (
|
|
15
|
-
<Prev
|
|
16
|
-
{...rest}
|
|
17
|
-
onClick={useEventCallback((e, item) => {
|
|
18
|
-
handle()
|
|
19
|
-
onClick?.(e, item)
|
|
20
|
-
})}
|
|
21
|
-
/>
|
|
22
|
-
)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const Plugin = GaProductListItemsBase
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ProductItemsGridProps } from '@graphcommerce/magento-product'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { GoogleAnalyticsItemList } from '../components/GoogleAnalyticsItemList'
|
|
4
|
-
|
|
5
|
-
export const component = 'ProductListItemsBase'
|
|
6
|
-
export const exported = '@graphcommerce/magento-product'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
export function GaProductListItemsBase(props: PluginProps<ProductItemsGridProps>) {
|
|
10
|
-
const { Prev, ...rest } = props
|
|
11
|
-
return (
|
|
12
|
-
<GoogleAnalyticsItemList {...rest}>
|
|
13
|
-
<Prev {...rest} />
|
|
14
|
-
</GoogleAnalyticsItemList>
|
|
15
|
-
)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const Plugin = GaProductListItemsBase
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { RemoveItemFromCart } from '@graphcommerce/magento-cart-items'
|
|
2
|
-
import { IfConfig, ReactPlugin } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagRemoveFromCart } from '../events/gtagRemoveFromCart/gtagRemoveFromCart'
|
|
4
|
-
|
|
5
|
-
export const component = 'RemoveItemFromCart'
|
|
6
|
-
export const exported = '@graphcommerce/magento-cart-items/components/RemoveItemFromCart/RemoveItemFromCart'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
export const GaRemoveItemFromCart: ReactPlugin<typeof RemoveItemFromCart> = (props) => {
|
|
10
|
-
const { Prev, uid, quantity, prices, product, buttonProps } = props
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<Prev
|
|
14
|
-
{...props}
|
|
15
|
-
product={product}
|
|
16
|
-
buttonProps={{
|
|
17
|
-
onClick: (e) => {
|
|
18
|
-
gtagRemoveFromCart({
|
|
19
|
-
__typename: 'Cart',
|
|
20
|
-
items: [{ uid, __typename: 'SimpleCartItem', product, quantity, prices }],
|
|
21
|
-
})
|
|
22
|
-
buttonProps?.onClick?.(e)
|
|
23
|
-
},
|
|
24
|
-
}}
|
|
25
|
-
/>
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const Plugin = GaRemoveItemFromCart
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { RemoveItemFromCartFab } from '@graphcommerce/magento-cart-items'
|
|
2
|
-
import { IfConfig, ReactPlugin } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagRemoveFromCart } from '../events/gtagRemoveFromCart/gtagRemoveFromCart'
|
|
4
|
-
|
|
5
|
-
export const component = 'RemoveItemFromCartFab'
|
|
6
|
-
export const exported = '@graphcommerce/magento-cart-items/components/RemoveItemFromCart/RemoveItemFromCartFab'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
export const GaRemoveItemFromCartFab: ReactPlugin<typeof RemoveItemFromCartFab> = (props) => {
|
|
10
|
-
const { Prev, uid, quantity, prices, product, fabProps } = props
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<Prev
|
|
14
|
-
{...props}
|
|
15
|
-
product={product}
|
|
16
|
-
fabProps={{
|
|
17
|
-
onClick: (e) => {
|
|
18
|
-
gtagRemoveFromCart({
|
|
19
|
-
__typename: 'Cart',
|
|
20
|
-
items: [{ uid, __typename: 'SimpleCartItem', product, quantity, prices }],
|
|
21
|
-
})
|
|
22
|
-
fabProps?.onClick?.(e)
|
|
23
|
-
},
|
|
24
|
-
}}
|
|
25
|
-
/>
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export const Plugin = GaRemoveItemFromCartFab
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { ShippingMethodFormProps } from '@graphcommerce/magento-cart-shipping-method'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { gtagAddShippingInfo } from '../events/gtagAddShippingInfo/gtagAddShippingInfo'
|
|
4
|
-
|
|
5
|
-
export const component = 'ShippingMethodForm'
|
|
6
|
-
export const exported = '@graphcommerce/magento-cart-shipping-method'
|
|
7
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
8
|
-
|
|
9
|
-
/** When the ShippingMethod is submitted the result is sent to Google Analytics */
|
|
10
|
-
export function GaShippingMethodForm(props: PluginProps<ShippingMethodFormProps>) {
|
|
11
|
-
const { Prev, onComplete, ...rest } = props
|
|
12
|
-
return (
|
|
13
|
-
<Prev
|
|
14
|
-
{...rest}
|
|
15
|
-
onComplete={(result, variables) => {
|
|
16
|
-
gtagAddShippingInfo(result.data?.setShippingMethodsOnCart?.cart)
|
|
17
|
-
return onComplete?.(result, variables)
|
|
18
|
-
}}
|
|
19
|
-
/>
|
|
20
|
-
)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const Plugin = GaShippingMethodForm
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { UpdateItemQuantityProps } from '@graphcommerce/magento-cart-items'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
|
|
4
|
-
export const component = 'UpdateItemQuantity'
|
|
5
|
-
export const exported = '@graphcommerce/magento-cart-items/components/UpdateItemQuantity/UpdateItemQuantity'
|
|
6
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* When a product is added to the Cart, by using the + button on cart page, send a Google Analytics
|
|
10
|
-
* event
|
|
11
|
-
*/
|
|
12
|
-
function GaUpdateItemQuantity(props: PluginProps<UpdateItemQuantityProps>) {
|
|
13
|
-
const { Prev, onComplete, quantity, ...rest } = props
|
|
14
|
-
|
|
15
|
-
return (
|
|
16
|
-
<Prev
|
|
17
|
-
{...rest}
|
|
18
|
-
quantity={quantity}
|
|
19
|
-
onComplete={(data, variables) => {
|
|
20
|
-
const original = onComplete?.(data, variables)
|
|
21
|
-
const diffQuantity = variables.quantity - quantity
|
|
22
|
-
if (diffQuantity === 0) return original
|
|
23
|
-
|
|
24
|
-
const itemId = variables.uid
|
|
25
|
-
const addedItem = data.data?.updateCartItems?.cart.items?.find(
|
|
26
|
-
(item) => item?.uid === itemId,
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
if (addedItem && addedItem.prices && addedItem.prices.row_total_including_tax.value) {
|
|
30
|
-
// we need to manually calculate pricePerItemInclTax (https://github.com/magento/magento2/issues/33848)
|
|
31
|
-
const pricePerItemInclTax =
|
|
32
|
-
addedItem.prices.row_total_including_tax.value / addedItem.quantity
|
|
33
|
-
const addToCartValue = pricePerItemInclTax * diffQuantity
|
|
34
|
-
|
|
35
|
-
globalThis.gtag?.('event', 'add_to_cart', {
|
|
36
|
-
currency: addedItem?.prices?.price.currency,
|
|
37
|
-
value: addToCartValue,
|
|
38
|
-
items: [
|
|
39
|
-
{
|
|
40
|
-
item_id: addedItem?.product.sku,
|
|
41
|
-
item_name: addedItem?.product.name,
|
|
42
|
-
currency: addedItem?.prices?.price.currency,
|
|
43
|
-
price: pricePerItemInclTax,
|
|
44
|
-
quantity: variables.quantity,
|
|
45
|
-
discount: addedItem?.prices?.discounts?.reduce(
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
47
|
-
(sum, discount) => sum + (discount?.amount?.value ?? 0),
|
|
48
|
-
0,
|
|
49
|
-
),
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return original
|
|
56
|
-
}}
|
|
57
|
-
/>
|
|
58
|
-
)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const Plugin = GaUpdateItemQuantity
|
package/plugins/GaViewItem.tsx
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ProductPageMeta } from '@graphcommerce/magento-product'
|
|
2
|
-
import { IfConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
-
import { useMemoObject } from '@graphcommerce/next-ui'
|
|
4
|
-
import { useEffect } from 'react'
|
|
5
|
-
import { productToGtagItem } from '../events/productToGtagItem/productToGtagItem'
|
|
6
|
-
|
|
7
|
-
export const component = 'ProductPageMeta'
|
|
8
|
-
export const exported = '@graphcommerce/magento-product'
|
|
9
|
-
export const ifConfig: IfConfig = 'googleAnalyticsId'
|
|
10
|
-
|
|
11
|
-
/** When a product is added to the Cart, send a Google Analytics event */
|
|
12
|
-
function GaViewItem(props: PluginProps<React.ComponentProps<typeof ProductPageMeta>>) {
|
|
13
|
-
const { Prev, product } = props
|
|
14
|
-
const { price_range } = product
|
|
15
|
-
|
|
16
|
-
const viewItem = useMemoObject({
|
|
17
|
-
currency: price_range.minimum_price.final_price.currency,
|
|
18
|
-
value: price_range.minimum_price.final_price.value,
|
|
19
|
-
items: [productToGtagItem(product)],
|
|
20
|
-
})
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
22
|
-
useEffect(() => globalThis.gtag?.('event', 'view_item', viewItem), [viewItem])
|
|
23
|
-
|
|
24
|
-
return <Prev {...props} />
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const Plugin = GaViewItem
|