@graphcommerce/google-datalayer 8.1.0-canary.9 → 9.0.0-canary.101

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 (40) hide show
  1. package/CHANGELOG.md +205 -10
  2. package/Config.graphqls +3 -0
  3. package/api/googleEventNames.ts +116 -0
  4. package/api/sendEvent.ts +15 -7
  5. package/components/DatalayerViewItemList.tsx +55 -6
  6. package/mapping/cartItemToDatalayerItem/cartItemToDatalayerItem.ts +3 -2
  7. package/mapping/cartItemToRemoveFromCart/cartToRemoveFromCart.ts +2 -5
  8. package/mapping/cartToAddPaymentInfo/Cart_AddPaymentInfo.graphql +1 -1
  9. package/mapping/cartToAddPaymentInfo/cartToAddPaymentInfo.ts +10 -3
  10. package/mapping/cartToAddShippingInfo/Cart_AddShippingInfo.graphql +1 -1
  11. package/mapping/cartToAddShippingInfo/cartToAddShippingInfo.ts +14 -6
  12. package/mapping/cartToBeginCheckout/Cart_BeginCheckout.graphql +1 -1
  13. package/mapping/cartToBeginCheckout/cartToBeginCheckout.ts +7 -3
  14. package/mapping/cartToPurchase/Cart_PurchaseEvent.graphql +18 -0
  15. package/mapping/cartToPurchase/cartToPurchase.ts +28 -0
  16. package/mapping/cartToViewCart/Cart_ViewCart.graphql +8 -1
  17. package/mapping/cartToViewCart/cartToViewCart.ts +12 -3
  18. package/mapping/datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue.ts +9 -2
  19. package/mapping/productItemsToViewItemList/productItemsToViewItemList.ts +28 -6
  20. package/mapping/productToDatalayerItem/Product_DatalayerItem.graphql +1 -6
  21. package/mapping/productToDatalayerItem/productToDatalayerItem.ts +11 -4
  22. package/mapping/productToViewItem/productToViewItem.ts +13 -8
  23. package/package.json +13 -10
  24. package/plugins/GoogleDatalayerAddProductsToCartForm.tsx +13 -9
  25. package/plugins/GoogleDatalayerCartStartCheckout.tsx +9 -8
  26. package/plugins/GoogleDatalayerCartStartCheckoutLinkOrButton.tsx +8 -7
  27. package/plugins/GoogleDatalayerPaymentMethodButton.tsx +8 -7
  28. package/plugins/GoogleDatalayerPaymentMethodContextProvider.tsx +11 -8
  29. package/plugins/GoogleDatalayerProductListItem.tsx +6 -6
  30. package/plugins/GoogleDatalayerProductListItemsBase.tsx +15 -9
  31. package/plugins/GoogleDatalayerRemoveItemFromCart.tsx +21 -22
  32. package/plugins/GoogleDatalayerShippingMethodForm.tsx +10 -8
  33. package/plugins/{GoogleDatalayerUpdateItemQuantity.tsx → GoogleDatalayerUseRemoveItemFromCart.tsx} +9 -9
  34. package/plugins/GoogleDatalayerUseSignInForm.tsx +0 -0
  35. package/plugins/GoogleDatalayerViewItem.tsx +10 -9
  36. package/plugins/GoogleDatalayerWebVitals.tsx +4 -4
  37. package/mapping/cartToDatalayerItems/Cart_DatalayerItems.graphql +0 -11
  38. package/mapping/cartToDatalayerItems/cartToDatalayerItems.ts +0 -10
  39. package/mapping/orderToPurchase/orderToPurchase.ts +0 -17
  40. package/plugins/GoogleDatalayerRemoveItemFromCartFab.tsx +0 -29
@@ -1,4 +1,11 @@
1
1
  fragment Cart_ViewCart on Cart @inject(into: ["CartItemCountChanged"]) {
2
2
  __typename
3
- ...Cart_DatalayerItems
3
+ items {
4
+ ...CartItem_DatalayerItem
5
+ }
6
+ prices {
7
+ subtotal_including_tax {
8
+ currency
9
+ }
10
+ }
4
11
  }
@@ -1,6 +1,15 @@
1
- import { cartToDatalayerItems } from '../cartToDatalayerItems/cartToDatalayerItems'
1
+ import { nonNullable } from '@graphcommerce/next-ui'
2
+ import { cartItemToDatalayerItem } from '../cartItemToDatalayerItem/cartItemToDatalayerItem'
3
+ import {
4
+ DataLayerCurrencyValue,
5
+ datalayerItemsToCurrencyValue,
6
+ } from '../datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue'
7
+ import { GoogleDatalayerItem } from '../productToDatalayerItem/productToDatalayerItem'
2
8
  import { Cart_ViewCartFragment } from './Cart_ViewCart.gql'
3
9
 
4
- export function cartToViewCart<C extends Cart_ViewCartFragment>(cart: C) {
5
- return cartToDatalayerItems(cart)
10
+ export type ViewCart = DataLayerCurrencyValue & { items: GoogleDatalayerItem[] }
11
+
12
+ export function cartToViewCart<C extends Cart_ViewCartFragment>(cart: C): ViewCart {
13
+ const items = cart?.items?.filter(nonNullable).map(cartItemToDatalayerItem) ?? []
14
+ return { ...datalayerItemsToCurrencyValue(items), items }
6
15
  }
@@ -1,8 +1,15 @@
1
1
  import { GoogleDatalayerItem } from '../productToDatalayerItem/productToDatalayerItem'
2
2
 
3
- export function datalayerItemsToCurrencyValue(items: GoogleDatalayerItem[]) {
3
+ export type DataLayerCurrencyValue = {
4
+ currency: string
5
+ value: number
6
+ }
7
+
8
+ export function datalayerItemsToCurrencyValue(
9
+ items: GoogleDatalayerItem[],
10
+ ): DataLayerCurrencyValue {
4
11
  return {
5
- currency: items[0].currency,
12
+ currency: items[0]?.currency ?? '',
6
13
  value: items.reduce((acc, item) => acc + (item.price ?? 0) * item.quantity, 0),
7
14
  }
8
15
  }
@@ -1,22 +1,44 @@
1
- import { ProductListItemFragment } from '@graphcommerce/magento-product'
1
+ import { ProductFilterParams, ProductListItemFragment } from '@graphcommerce/magento-product'
2
2
  import { nonNullable } from '@graphcommerce/next-ui'
3
- import { productToDatalayerItem } from '../productToDatalayerItem/productToDatalayerItem'
3
+ import {
4
+ GoogleDatalayerItem,
5
+ productToDatalayerItem,
6
+ } from '../productToDatalayerItem/productToDatalayerItem'
7
+
8
+ export type ViewItemList = {
9
+ item_list_id: string
10
+ item_list_name: string
11
+ items: GoogleDatalayerItem[]
12
+ filter_params?: ProductFilterParams | null
13
+ }
14
+
15
+ export type SelectItem = {
16
+ item_list_id: string
17
+ item_list_name: string
18
+ items: GoogleDatalayerItem[]
19
+ filter_params?: ProductFilterParams | null
20
+ }
4
21
 
5
22
  export function productItemsToViewItemList<P extends ProductListItemFragment>(
6
23
  item_list_id: string,
7
24
  item_list_name: string,
8
25
  items: Array<P | null | undefined> | null | undefined,
9
- ) {
26
+ filter_params: ProductFilterParams | null | undefined,
27
+ ): ViewItemList {
10
28
  return {
11
29
  item_list_id,
12
30
  item_list_name,
13
- items: items?.filter(nonNullable)?.map((item) => productToDatalayerItem(item)) ?? [],
31
+ items: (items ?? [])?.filter(nonNullable)?.map(productToDatalayerItem),
32
+ filter_params,
14
33
  }
15
34
  }
16
35
 
17
36
  export function viewItemListToSelectItem(
18
37
  viewItemList: ReturnType<typeof productItemsToViewItemList>,
19
38
  itemId: string,
20
- ) {
21
- return { ...viewItemList, items: viewItemList.items.filter((i) => i.item_id === itemId) }
39
+ ): SelectItem {
40
+ return {
41
+ ...viewItemList,
42
+ items: viewItemList.items.filter((i) => i.item_id === itemId),
43
+ }
22
44
  }
@@ -4,12 +4,7 @@ fragment Product_DatalayerItem on ProductInterface {
4
4
  sku
5
5
  price_range {
6
6
  minimum_price {
7
- final_price {
8
- ...Money
9
- }
10
- discount {
11
- amount_off
12
- }
7
+ ...ProductListPrice
13
8
  }
14
9
  }
15
10
  ...ProductPageBreadcrumb
@@ -2,14 +2,18 @@ import { productPageCategory } from '@graphcommerce/magento-product'
2
2
  import { nonNullable } from '@graphcommerce/next-ui'
3
3
  import { Product_DatalayerItemFragment } from './Product_DatalayerItem.gql'
4
4
 
5
+ /**
6
+ * https://developers.google.com/tag-platform/gtagjs/reference/events#add_to_cart_item
7
+ */
5
8
  export type GoogleDatalayerItem = {
6
9
  item_id: string
10
+ item_uid: string
7
11
  item_name: string
8
12
  affiliation?: string
9
13
  coupon?: string
10
14
  currency?: string
11
15
  discount?: number
12
- index?: number
16
+ index: number
13
17
  item_brand?: string
14
18
  item_category?: string
15
19
  item_category2?: string
@@ -20,27 +24,30 @@ export type GoogleDatalayerItem = {
20
24
  item_list_name?: string
21
25
  item_variant?: string
22
26
  location_id?: string
23
- price?: number
27
+ price: number
24
28
  quantity: number
25
29
  }
26
30
 
27
31
  export function productToDatalayerItem<P extends Product_DatalayerItemFragment>(
28
32
  item: P,
33
+ index: number,
29
34
  ): GoogleDatalayerItem {
30
35
  const category = productPageCategory(item)
31
36
  const item_categories = Object.fromEntries(
32
37
  [...(category?.breadcrumbs?.map((b) => b?.category_name) ?? []), category?.name]
33
38
  .filter(nonNullable)
34
- .map((name, index) => [`item_category${index > 0 ? index + 1 : ''}`, name]),
39
+ .map((name, idx) => [`item_category${idx > 0 ? idx + 1 : ''}`, name]),
35
40
  )
36
41
 
37
42
  return {
38
43
  item_id: item.sku ?? '',
44
+ item_uid: item.uid,
39
45
  item_name: item.name ?? '',
40
- price: item.price_range?.minimum_price.final_price.value ?? undefined,
46
+ price: item.price_range?.minimum_price.final_price.value ?? 0,
41
47
  currency: item.price_range?.minimum_price.final_price.currency ?? undefined,
42
48
  discount: item.price_range?.minimum_price.discount?.amount_off ?? undefined,
43
49
  quantity: 1,
50
+ index,
44
51
  ...item_categories,
45
52
  }
46
53
  }
@@ -1,11 +1,16 @@
1
- import { datalayerItemsToCurrencyValue } from '../datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue'
2
- import { productToDatalayerItem } from '../productToDatalayerItem/productToDatalayerItem'
1
+ import {
2
+ DataLayerCurrencyValue,
3
+ datalayerItemsToCurrencyValue,
4
+ } from '../datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue'
5
+ import {
6
+ GoogleDatalayerItem,
7
+ productToDatalayerItem,
8
+ } from '../productToDatalayerItem/productToDatalayerItem'
3
9
  import { Product_ViewItemFragment } from './Product_ViewItem.gql'
4
10
 
5
- export function productToViewItem<C extends Product_ViewItemFragment>(product: C) {
6
- const items = [productToDatalayerItem(product)]
7
- return {
8
- ...datalayerItemsToCurrencyValue(items),
9
- items,
10
- }
11
+ export type ViewItem = { items: GoogleDatalayerItem[] } & DataLayerCurrencyValue
12
+
13
+ export function productToViewItem<C extends Product_ViewItemFragment>(product: C): ViewItem {
14
+ const items = [productToDatalayerItem(product, 0)]
15
+ return { ...datalayerItemsToCurrencyValue(items), items }
11
16
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/google-datalayer",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.9",
5
+ "version": "9.0.0-canary.101",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,14 +12,14 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.9",
16
- "@graphcommerce/magento-cart": "^8.1.0-canary.9",
17
- "@graphcommerce/magento-cart-payment-method": "^8.1.0-canary.9",
18
- "@graphcommerce/magento-cart-shipping-method": "^8.1.0-canary.9",
19
- "@graphcommerce/magento-product": "^8.1.0-canary.9",
20
- "@graphcommerce/next-ui": "^8.1.0-canary.9",
21
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.9",
22
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.9",
15
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.101",
16
+ "@graphcommerce/magento-cart": "^9.0.0-canary.101",
17
+ "@graphcommerce/magento-cart-payment-method": "^9.0.0-canary.101",
18
+ "@graphcommerce/magento-cart-shipping-method": "^9.0.0-canary.101",
19
+ "@graphcommerce/magento-product": "^9.0.0-canary.101",
20
+ "@graphcommerce/next-ui": "^9.0.0-canary.101",
21
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.101",
22
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.101",
23
23
  "@mui/material": "^5.14.20",
24
24
  "next": "^14",
25
25
  "react": "^18.2.0",
@@ -37,9 +37,12 @@
37
37
  },
38
38
  "@graphcommerce/magento-product": {
39
39
  "optional": true
40
+ },
41
+ "@graphcommerce/magento-product-configurable": {
42
+ "optional": true
40
43
  }
41
44
  },
42
45
  "dependencies": {
43
- "web-vitals": "^3.5.2"
46
+ "web-vitals": "^4.2.3"
44
47
  }
45
48
  }
@@ -3,19 +3,22 @@ import {
3
3
  findAddedItems,
4
4
  toUserErrors,
5
5
  } from '@graphcommerce/magento-product'
6
- import type { PluginProps } from '@graphcommerce/next-config'
6
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
7
7
  import { nonNullable } from '@graphcommerce/next-ui'
8
- import { sendEvent } from '../api/sendEvent'
8
+ import { useSendEvent } from '../api/sendEvent'
9
9
  import { cartItemToDatalayerItem } from '../mapping/cartItemToDatalayerItem/cartItemToDatalayerItem'
10
10
  import { datalayerItemsToCurrencyValue } from '../mapping/datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue'
11
11
 
12
- export const component = 'AddProductsToCartForm'
13
- export const exported = '@graphcommerce/magento-product'
12
+ export const config: PluginConfig = {
13
+ module: '@graphcommerce/magento-product',
14
+ type: 'component',
15
+ }
14
16
 
15
17
  /** When a product is added to the Cart, send a Google Analytics event */
16
- function GoogleDatalayerAddProductsToCartForm(props: PluginProps<AddProductsToCartFormProps>) {
18
+ export function AddProductsToCartForm(props: PluginProps<AddProductsToCartFormProps>) {
17
19
  const { Prev, onComplete, ...rest } = props
18
20
 
21
+ const sendEvent = useSendEvent()
19
22
  return (
20
23
  <Prev
21
24
  {...rest}
@@ -24,9 +27,12 @@ function GoogleDatalayerAddProductsToCartForm(props: PluginProps<AddProductsToCa
24
27
  const addedItems = findAddedItems(data, variables)
25
28
 
26
29
  const items = addedItems
27
- .map(({ itemVariable, itemInCart }) => {
30
+ .map(({ itemVariable, itemInCart }, index) => {
28
31
  if (!itemInCart) return null
29
- return { ...cartItemToDatalayerItem(itemInCart), quantity: itemVariable.quantity }
32
+ return {
33
+ ...cartItemToDatalayerItem(itemInCart, index),
34
+ quantity: itemVariable.quantity,
35
+ }
30
36
  })
31
37
  .filter(nonNullable)
32
38
 
@@ -48,5 +54,3 @@ function GoogleDatalayerAddProductsToCartForm(props: PluginProps<AddProductsToCa
48
54
  />
49
55
  )
50
56
  }
51
-
52
- export const Plugin = GoogleDatalayerAddProductsToCartForm
@@ -1,25 +1,28 @@
1
1
  import { CartStartCheckoutProps } from '@graphcommerce/magento-cart'
2
- import type { PluginProps } from '@graphcommerce/next-config'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
3
  import { useMemoObject } from '@graphcommerce/next-ui'
4
4
  import { useEffect, useRef } from 'react'
5
- import { sendEvent } from '../api/sendEvent'
5
+ import { useSendEvent } from '../api/sendEvent'
6
6
  import { cartToBeginCheckout } from '../mapping/cartToBeginCheckout/cartToBeginCheckout'
7
7
  import { cartToViewCart } from '../mapping/cartToViewCart/cartToViewCart'
8
8
 
9
- export const component = 'CartStartCheckout'
10
- export const exported = '@graphcommerce/magento-cart'
9
+ export const config: PluginConfig = {
10
+ module: '@graphcommerce/magento-cart',
11
+ type: 'component',
12
+ }
11
13
 
12
- export function GoogleDatalayerCartStartCheckout(props: PluginProps<CartStartCheckoutProps>) {
14
+ export function CartStartCheckout(props: PluginProps<CartStartCheckoutProps>) {
13
15
  const { Prev, onStart, ...rest } = props
14
16
 
15
17
  const send = useRef(false)
18
+ const sendEvent = useSendEvent()
16
19
  const viewCart = useMemoObject(cartToViewCart({ __typename: 'Cart', ...props }))
17
20
  useEffect(() => {
18
21
  if (!send.current) {
19
22
  sendEvent('view_cart', viewCart)
20
23
  send.current = true
21
24
  }
22
- }, [viewCart])
25
+ }, [sendEvent, viewCart])
23
26
 
24
27
  return (
25
28
  <Prev
@@ -31,5 +34,3 @@ export function GoogleDatalayerCartStartCheckout(props: PluginProps<CartStartChe
31
34
  />
32
35
  )
33
36
  }
34
-
35
- export const Plugin = GoogleDatalayerCartStartCheckout
@@ -1,16 +1,19 @@
1
1
  import { CartStartCheckoutLinkOrButtonProps } from '@graphcommerce/magento-cart'
2
- import type { PluginProps } from '@graphcommerce/next-config'
3
- import { sendEvent } from '../api/sendEvent'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
+ import { useSendEvent } from '../api/sendEvent'
4
4
  import { cartToBeginCheckout } from '../mapping/cartToBeginCheckout/cartToBeginCheckout'
5
5
 
6
- export const component = 'CartStartCheckoutLinkOrButton'
7
- export const exported = '@graphcommerce/magento-cart'
6
+ export const config: PluginConfig = {
7
+ module: '@graphcommerce/magento-cart',
8
+ type: 'component',
9
+ }
8
10
 
9
- export function GoogleDatalayerCartStartCheckoutLinkOrButton(
11
+ export function CartStartCheckoutLinkOrButton(
10
12
  props: PluginProps<CartStartCheckoutLinkOrButtonProps>,
11
13
  ) {
12
14
  const { Prev, onStart, ...rest } = props
13
15
 
16
+ const sendEvent = useSendEvent()
14
17
  return (
15
18
  <Prev
16
19
  {...rest}
@@ -21,5 +24,3 @@ export function GoogleDatalayerCartStartCheckoutLinkOrButton(
21
24
  />
22
25
  )
23
26
  }
24
-
25
- export const Plugin = GoogleDatalayerCartStartCheckoutLinkOrButton
@@ -1,17 +1,20 @@
1
1
  import { useCartQuery } from '@graphcommerce/magento-cart'
2
2
  import { PaymentMethodButtonProps } from '@graphcommerce/magento-cart-payment-method'
3
3
  import { GetPaymentMethodContextDocument } from '@graphcommerce/magento-cart-payment-method/PaymentMethodContext/GetPaymentMethodContext.gql'
4
- import type { PluginProps } from '@graphcommerce/next-config'
5
- import { sendEvent } from '../api/sendEvent'
4
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
5
+ import { useSendEvent } from '../api/sendEvent'
6
6
  import { cartToAddPaymentInfo } from '../mapping/cartToAddPaymentInfo/cartToAddPaymentInfo'
7
7
 
8
- export const component = 'PaymentMethodButton'
9
- export const exported = '@graphcommerce/magento-cart-payment-method'
8
+ export const config: PluginConfig = {
9
+ module: '@graphcommerce/magento-cart-payment-method',
10
+ type: 'component',
11
+ }
10
12
 
11
- function GoogleDatalayerPaymentMethodButton(props: PluginProps<PaymentMethodButtonProps>) {
13
+ export function PaymentMethodButton(props: PluginProps<PaymentMethodButtonProps>) {
12
14
  const { Prev, onSubmitSuccessful, ...rest } = props
13
15
  const methodContext = useCartQuery(GetPaymentMethodContextDocument)
14
16
 
17
+ const sendEvent = useSendEvent()
15
18
  return (
16
19
  <Prev
17
20
  {...rest}
@@ -24,5 +27,3 @@ function GoogleDatalayerPaymentMethodButton(props: PluginProps<PaymentMethodButt
24
27
  />
25
28
  )
26
29
  }
27
-
28
- export const Plugin = GoogleDatalayerPaymentMethodButton
@@ -1,23 +1,26 @@
1
1
  import type { PaymentMethodContextProviderProps } from '@graphcommerce/magento-cart-payment-method'
2
- import type { PluginProps } from '@graphcommerce/next-config'
3
- import { sendEvent } from '../api/sendEvent'
4
- import { orderToPurchase } from '../mapping/orderToPurchase/orderToPurchase'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
+ import { useSendEvent } from '../api/sendEvent'
4
+ import { cartToPurchase } from '../mapping/cartToPurchase/cartToPurchase'
5
5
 
6
- export const component = 'PaymentMethodContextProvider'
7
- export const exported = '@graphcommerce/magento-cart-payment-method'
6
+ export const config: PluginConfig = {
7
+ module: '@graphcommerce/magento-cart-payment-method',
8
+ type: 'component',
9
+ }
8
10
 
9
- function GoogleDatalayerPaymentMethodContextProvider(
11
+ export function PaymentMethodContextProvider(
10
12
  props: PluginProps<PaymentMethodContextProviderProps>,
11
13
  ) {
12
14
  const { Prev, onSuccess, ...rest } = props
15
+
16
+ const sendEvent = useSendEvent()
13
17
  return (
14
18
  <Prev
15
19
  {...rest}
16
20
  onSuccess={(orderNumber, cart) => {
17
- sendEvent('purchase', orderToPurchase(orderNumber, cart))
21
+ sendEvent('purchase', cartToPurchase(orderNumber, cart))
18
22
  return onSuccess?.(orderNumber, cart)
19
23
  }}
20
24
  />
21
25
  )
22
26
  }
23
- export const Plugin = GoogleDatalayerPaymentMethodContextProvider
@@ -1,12 +1,14 @@
1
1
  import { ProductListItemProps } from '@graphcommerce/magento-product'
2
- import type { PluginProps } from '@graphcommerce/next-config'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
3
  import { useEventCallback } from '@mui/material'
4
4
  import { useViewItemList } from '../components/DatalayerViewItemList'
5
5
 
6
- export const component = 'ProductListItem'
7
- export const exported = '@graphcommerce/magento-product'
6
+ export const config: PluginConfig = {
7
+ module: '@graphcommerce/magento-product',
8
+ type: 'component',
9
+ }
8
10
 
9
- function GoogleDatalayerProductListItem(props: PluginProps<ProductListItemProps>) {
11
+ export function ProductListItem(props: PluginProps<ProductListItemProps>) {
10
12
  const { Prev, onClick, ...rest } = props
11
13
  const selectItem = useViewItemList()
12
14
 
@@ -20,5 +22,3 @@ function GoogleDatalayerProductListItem(props: PluginProps<ProductListItemProps>
20
22
  />
21
23
  )
22
24
  }
23
-
24
- export const Plugin = GoogleDatalayerProductListItem
@@ -1,17 +1,23 @@
1
1
  import type { ProductItemsGridProps } from '@graphcommerce/magento-product'
2
- import type { PluginProps } from '@graphcommerce/next-config'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
+ import { useForkRef } from '@mui/material'
4
+ import { useRef } from 'react'
3
5
  import { DatalayerViewItemList } from '../components/DatalayerViewItemList'
4
6
 
5
- export const component = 'ProductListItemsBase'
6
- export const exported = '@graphcommerce/magento-product'
7
+ export const config: PluginConfig = {
8
+ module: '@graphcommerce/magento-product',
9
+ type: 'component',
10
+ }
11
+
12
+ export function ProductListItemsBase(props: PluginProps<ProductItemsGridProps>) {
13
+ const { Prev, containerRef, ...rest } = props
14
+
15
+ const internalRef = useRef<HTMLDivElement>(null)
16
+ const ref = useForkRef(containerRef, internalRef)
7
17
 
8
- export function GoogleDatalayerProductListItemsBase(props: PluginProps<ProductItemsGridProps>) {
9
- const { Prev, ...rest } = props
10
18
  return (
11
- <DatalayerViewItemList {...rest}>
12
- <Prev {...rest} />
19
+ <DatalayerViewItemList {...rest} containerRef={internalRef}>
20
+ <Prev {...rest} containerRef={ref} />
13
21
  </DatalayerViewItemList>
14
22
  )
15
23
  }
16
-
17
- export const Plugin = GoogleDatalayerProductListItemsBase
@@ -1,29 +1,28 @@
1
- import type { RemoveItemFromCartProps } from '@graphcommerce/magento-cart-items'
2
- import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
- import { sendEvent } from '../api/sendEvent'
1
+ import type { useRemoveItemFromCart as useRemoveItemFromCartBase } from '@graphcommerce/magento-cart-items'
2
+ import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
+ import { useSendEvent } from '../api/sendEvent'
4
4
  import { cartItemToRemoveFromCart } from '../mapping/cartItemToRemoveFromCart/cartToRemoveFromCart'
5
5
 
6
6
  export const config: PluginConfig = {
7
- type: 'component',
7
+ type: 'function',
8
8
  module: '@graphcommerce/magento-cart-items',
9
9
  }
10
10
 
11
- export function RemoveItemFromCart(props: PluginProps<RemoveItemFromCartProps>) {
12
- const { Prev, buttonProps } = props
13
-
14
- return (
15
- <Prev
16
- {...props}
17
- buttonProps={{
18
- ...buttonProps,
19
- onClick: (e) => {
20
- sendEvent(
21
- 'remove_from_cart',
22
- cartItemToRemoveFromCart({ __typename: 'SimpleCartItem', ...props }),
23
- )
24
- buttonProps?.onClick?.(e)
25
- },
26
- }}
27
- />
28
- )
11
+ export const useRemoveItemFromCart: FunctionPlugin<typeof useRemoveItemFromCartBase> = (
12
+ usePrev,
13
+ props,
14
+ ) => {
15
+ const sendEvent = useSendEvent()
16
+ return usePrev({
17
+ ...props,
18
+ onComplete: (result, variables) => {
19
+ if (!result.errors) {
20
+ sendEvent(
21
+ 'remove_from_cart',
22
+ cartItemToRemoveFromCart({ ...props, __typename: 'SimpleCartItem' }),
23
+ )
24
+ }
25
+ return props.onComplete?.(result, variables)
26
+ },
27
+ })
29
28
  }
@@ -1,13 +1,17 @@
1
1
  import { ShippingMethodFormProps } from '@graphcommerce/magento-cart-shipping-method'
2
- import type { PluginProps } from '@graphcommerce/next-config'
3
- import { sendEvent } from '../api/sendEvent'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
+ import { useSendEvent } from '../api/sendEvent'
4
4
  import { cartToAddShippingInfo } from '../mapping/cartToAddShippingInfo/cartToAddShippingInfo'
5
5
 
6
- export const component = 'ShippingMethodForm'
7
- export const exported = '@graphcommerce/magento-cart-shipping-method'
6
+ export const config: PluginConfig = {
7
+ module: '@graphcommerce/magento-cart-shipping-method',
8
+ type: 'component',
9
+ }
8
10
 
9
- export function GoogleDatalayerShippingMethodForm(props: PluginProps<ShippingMethodFormProps>) {
11
+ export function ShippingMethodForm(props: PluginProps<ShippingMethodFormProps>) {
10
12
  const { Prev, onComplete, ...rest } = props
13
+
14
+ const sendEvent = useSendEvent()
11
15
  return (
12
16
  <Prev
13
17
  {...rest}
@@ -15,7 +19,7 @@ export function GoogleDatalayerShippingMethodForm(props: PluginProps<ShippingMet
15
19
  if (result.data?.setShippingMethodsOnCart?.cart) {
16
20
  sendEvent(
17
21
  'add_shipping_info',
18
- cartToAddShippingInfo(result.data?.setShippingMethodsOnCart?.cart),
22
+ cartToAddShippingInfo(result.data.setShippingMethodsOnCart.cart),
19
23
  )
20
24
  }
21
25
 
@@ -24,5 +28,3 @@ export function GoogleDatalayerShippingMethodForm(props: PluginProps<ShippingMet
24
28
  />
25
29
  )
26
30
  }
27
-
28
- export const Plugin = GoogleDatalayerShippingMethodForm
@@ -1,20 +1,22 @@
1
1
  import type { UpdateItemQuantityProps } from '@graphcommerce/magento-cart-items'
2
- import type { PluginProps } from '@graphcommerce/next-config'
3
- import { sendEvent } from '../api/sendEvent'
2
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
+ import { useSendEvent } from '../api/sendEvent'
4
4
  import { cartItemToDatalayerItem } from '../mapping/cartItemToDatalayerItem/cartItemToDatalayerItem'
5
5
  import { datalayerItemsToCurrencyValue } from '../mapping/datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue'
6
6
 
7
- export const component = 'UpdateItemQuantity'
8
- export const exported =
9
- '@graphcommerce/magento-cart-items/components/UpdateItemQuantity/UpdateItemQuantity'
7
+ export const config: PluginConfig = {
8
+ module: '@graphcommerce/magento-cart-items',
9
+ type: 'component',
10
+ }
10
11
 
11
12
  /**
12
13
  * When a product is added to the Cart, by using the + button on cart page, send a Google Analytics
13
14
  * event
14
15
  */
15
- function GoogleDatalayerUpdateItemQuantity(props: PluginProps<UpdateItemQuantityProps>) {
16
+ export function UpdateItemQuantity(props: PluginProps<UpdateItemQuantityProps>) {
16
17
  const { Prev, formOptions, quantity, ...rest } = props
17
18
 
19
+ const sendEvent = useSendEvent()
18
20
  return (
19
21
  <Prev
20
22
  {...rest}
@@ -32,7 +34,7 @@ function GoogleDatalayerUpdateItemQuantity(props: PluginProps<UpdateItemQuantity
32
34
 
33
35
  if (!itemInCart?.quantity || diffQuantity === 0) return original
34
36
 
35
- const items = [{ ...cartItemToDatalayerItem(itemInCart), quantity: absQuantity }]
37
+ const items = [{ ...cartItemToDatalayerItem(itemInCart, 0), quantity: absQuantity }]
36
38
  sendEvent(diffQuantity < 0 ? 'remove_from_cart' : 'add_to_cart', {
37
39
  ...datalayerItemsToCurrencyValue(items),
38
40
  items,
@@ -44,5 +46,3 @@ function GoogleDatalayerUpdateItemQuantity(props: PluginProps<UpdateItemQuantity
44
46
  />
45
47
  )
46
48
  }
47
-
48
- export const Plugin = GoogleDatalayerUpdateItemQuantity
File without changes