@graphcommerce/google-datalayer 9.0.0-canary.73 → 9.0.0-canary.74

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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # @graphcommerce/google-datalayer
2
2
 
3
+ ## 9.0.0-canary.74
4
+
3
5
  ## 9.0.0-canary.73
4
6
 
5
7
  ### Minor Changes
@@ -1,12 +1,13 @@
1
1
  import { ProductItemsGridProps, useProductFiltersPro } from '@graphcommerce/magento-product'
2
2
  import { useMemoObject } from '@graphcommerce/next-ui'
3
3
  import { useEventCallback } from '@mui/material'
4
- import React, { useContext, useEffect } from 'react'
4
+ import React, { useContext, useEffect, useRef } from 'react'
5
5
  import { useSendEvent } from '../api/sendEvent'
6
6
  import {
7
7
  productItemsToViewItemList,
8
8
  viewItemListToSelectItem,
9
9
  } from '../mapping/productItemsToViewItemList/productItemsToViewItemList'
10
+ import { useDebounce } from '@graphcommerce/react-hook-form'
10
11
 
11
12
  const DatalayerSelectItemContext = React.createContext<((itemId: string) => void) | undefined>(
12
13
  undefined,
@@ -25,7 +26,7 @@ export function useViewItemList() {
25
26
  export function DatalayerViewItemList(
26
27
  props: ProductItemsGridProps & { children: React.ReactNode },
27
28
  ) {
28
- const { title: item_list_name, items, children } = props
29
+ const { title: item_list_name, items, children, containerRef } = props
29
30
  const item_list_id = item_list_name.toLowerCase().replace(/\s/g, '_')
30
31
 
31
32
  const params = useProductFiltersPro(true)?.params
@@ -34,7 +35,52 @@ export function DatalayerViewItemList(
34
35
  const viewItemList = useMemoObject(
35
36
  productItemsToViewItemList(item_list_id, item_list_name, items, params),
36
37
  )
37
- useEffect(() => sendEvent('view_item_list', viewItemList), [sendEvent, viewItemList])
38
+
39
+ const sendUids = useRef<Map<string, boolean>>(new Map())
40
+
41
+ const send = useDebounce(() => {
42
+ const toSend = [...sendUids.current.entries()]
43
+ .filter(([, value]) => value !== true)
44
+ .map(([key]) => key)
45
+ if (toSend.length === 0) return
46
+
47
+ const viewItems = viewItemList.items.filter((item) => toSend.includes(item.item_uid))
48
+
49
+ sendEvent('view_item_list', { ...viewItemList, items: viewItems })
50
+
51
+ // Mark as send.
52
+ toSend.forEach((item_uid) => sendUids.current.set(item_uid, true))
53
+ }, 1000)
54
+
55
+ useEffect(() => {
56
+ sendUids.current.clear()
57
+ if (typeof containerRef === 'function' || !containerRef?.current) return () => {}
58
+
59
+ const childNodes = [...containerRef.current.childNodes].filter((n) => n instanceof HTMLElement)
60
+
61
+ const io = new IntersectionObserver(
62
+ (entries) => {
63
+ for (const entry of entries) {
64
+ const { target } = entry
65
+ if (entry.isIntersecting && target instanceof HTMLElement) {
66
+ const index = childNodes.indexOf(target)
67
+ const item = viewItemList.items[index]
68
+ if (item && !sendUids.current.has(item.item_uid)) {
69
+ sendUids.current.set(item.item_uid, false)
70
+ }
71
+ }
72
+ }
73
+ send()
74
+ },
75
+ { threshold: 0.5 },
76
+ )
77
+
78
+ childNodes.forEach((node) => {
79
+ io.observe(node)
80
+ })
81
+
82
+ return () => io.disconnect()
83
+ }, [containerRef, send, viewItemList])
38
84
 
39
85
  const selectItem = useEventCallback((itemId: string) => {
40
86
  sendEvent('select_item', viewItemListToSelectItem(viewItemList, itemId))
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": "9.0.0-canary.73",
5
+ "version": "9.0.0-canary.74",
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": "^9.0.0-canary.73",
16
- "@graphcommerce/magento-cart": "^9.0.0-canary.73",
17
- "@graphcommerce/magento-cart-payment-method": "^9.0.0-canary.73",
18
- "@graphcommerce/magento-cart-shipping-method": "^9.0.0-canary.73",
19
- "@graphcommerce/magento-product": "^9.0.0-canary.73",
20
- "@graphcommerce/next-ui": "^9.0.0-canary.73",
21
- "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.73",
22
- "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.73",
15
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.74",
16
+ "@graphcommerce/magento-cart": "^9.0.0-canary.74",
17
+ "@graphcommerce/magento-cart-payment-method": "^9.0.0-canary.74",
18
+ "@graphcommerce/magento-cart-shipping-method": "^9.0.0-canary.74",
19
+ "@graphcommerce/magento-product": "^9.0.0-canary.74",
20
+ "@graphcommerce/next-ui": "^9.0.0-canary.74",
21
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.74",
22
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.74",
23
23
  "@mui/material": "^5.14.20",
24
24
  "next": "^14",
25
25
  "react": "^18.2.0",
@@ -1,5 +1,7 @@
1
1
  import type { ProductItemsGridProps } from '@graphcommerce/magento-product'
2
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
7
  export const config: PluginConfig = {
@@ -8,10 +10,14 @@ export const config: PluginConfig = {
8
10
  }
9
11
 
10
12
  export function ProductListItemsBase(props: PluginProps<ProductItemsGridProps>) {
11
- const { Prev, ...rest } = props
13
+ const { Prev, containerRef, ...rest } = props
14
+
15
+ const internalRef = useRef<HTMLDivElement>(null)
16
+ const ref = useForkRef(containerRef, internalRef)
17
+
12
18
  return (
13
- <DatalayerViewItemList {...rest}>
14
- <Prev {...rest} />
19
+ <DatalayerViewItemList {...rest} containerRef={internalRef}>
20
+ <Prev {...rest} containerRef={ref} />
15
21
  </DatalayerViewItemList>
16
22
  )
17
23
  }