@graphcommerce/googleanalytics 5.1.0-canary.7 → 5.1.0-canary.8

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,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 5.1.0-canary.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1757](https://github.com/graphcommerce-org/graphcommerce/pull/1757) [`566beeee3`](https://github.com/graphcommerce-org/graphcommerce/commit/566beeee3b8300e836cb4cdb8102f1a239c281d7) - Make sure we can handle deeply nested items in a list ([@paales](https://github.com/paales))
8
+
3
9
  ## 5.1.0-canary.7
4
10
 
5
11
  ## 5.1.0-canary.6
@@ -0,0 +1,67 @@
1
+ import { nonNullable, useMemoDeep } 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 = useMemoDeep(
43
+ () => ({
44
+ item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
45
+ item_list_name: title,
46
+ items:
47
+ items?.map((item) => (item ? productToGtagItem(item) : null)).filter(nonNullable) ?? [],
48
+ }),
49
+ [listId, items, title],
50
+ )
51
+
52
+ useEffect(() => globalThis.gtag?.('event', 'view_item_list', eventData), [eventData])
53
+
54
+ const value = useMemo(
55
+ () => ({
56
+ item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
57
+ item_list_name: title ?? listId,
58
+ }),
59
+ [listId, title],
60
+ )
61
+
62
+ return (
63
+ <GoogleAnalyticsItemListContext.Provider value={value}>
64
+ {children}
65
+ </GoogleAnalyticsItemListContext.Provider>
66
+ )
67
+ }
@@ -1 +1,2 @@
1
1
  export * from './GoogleAnalyticsScript'
2
+ export * from './GoogleAnalyticsItemList'
package/index.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  export * from './components'
2
- export * from './events/useGtagViewItemList/useGtagViewItemList'
3
2
  export * from './events/gtagBeginCheckout/gtagBeginCheckout'
4
3
  export * from './events/gtagAddPaymentInfo/gtagAddPaymentInfo'
5
4
  export * from './events/gtagAddPurchaseInfo/gtagAddPurchaseInfo'
6
5
  export * from './events/gtagAddShippingInfo/gtagAddShippingInfo'
7
- export * from './events/gtagSelectItem/gtagSelectItem'
8
- export * from './events/useGtagPurchase/useGtagPurchase'
9
6
  export * from './events/gtagAddToCart/gtagAddToCart'
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": "5.1.0-canary.7",
5
+ "version": "5.1.0-canary.8",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,18 +12,18 @@
12
12
  }
13
13
  },
14
14
  "dependencies": {
15
- "@graphcommerce/graphql-mesh": "5.1.0-canary.7",
16
- "@graphcommerce/magento-cart": "5.1.0-canary.7",
17
- "@graphcommerce/magento-cart-payment-method": "5.1.0-canary.7",
18
- "@graphcommerce/magento-cart-shipping-method": "5.1.0-canary.7",
19
- "@graphcommerce/magento-product": "5.1.0-canary.7",
20
- "@graphcommerce/next-config": "5.1.0-canary.7",
21
- "@graphcommerce/next-ui": "5.1.0-canary.7"
15
+ "@graphcommerce/graphql-mesh": "5.1.0-canary.8",
16
+ "@graphcommerce/magento-cart": "5.1.0-canary.8",
17
+ "@graphcommerce/magento-cart-payment-method": "5.1.0-canary.8",
18
+ "@graphcommerce/magento-cart-shipping-method": "5.1.0-canary.8",
19
+ "@graphcommerce/magento-product": "5.1.0-canary.8",
20
+ "@graphcommerce/next-config": "5.1.0-canary.8",
21
+ "@graphcommerce/next-ui": "5.1.0-canary.8"
22
22
  },
23
23
  "devDependencies": {
24
- "@graphcommerce/eslint-config-pwa": "5.1.0-canary.7",
25
- "@graphcommerce/prettier-config-pwa": "5.1.0-canary.7",
26
- "@graphcommerce/typescript-config-pwa": "5.1.0-canary.7",
24
+ "@graphcommerce/eslint-config-pwa": "5.1.0-canary.8",
25
+ "@graphcommerce/prettier-config-pwa": "5.1.0-canary.8",
26
+ "@graphcommerce/typescript-config-pwa": "5.1.0-canary.8",
27
27
  "@types/gtag.js": "^0.0.12"
28
28
  },
29
29
  "peerDependencies": {
@@ -0,0 +1,24 @@
1
+ import { ProductListItemProps } from '@graphcommerce/magento-product'
2
+ import { 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
+
9
+ function GaProductListItemsBase(props: PluginProps<ProductListItemProps>) {
10
+ const { Prev, onClick, ...rest } = props
11
+ const handle = useGoogleAnalyticsListItemHandler(rest)
12
+
13
+ return (
14
+ <Prev
15
+ {...rest}
16
+ onClick={useEventCallback((e, item) => {
17
+ handle()
18
+ onClick?.(e, item)
19
+ })}
20
+ />
21
+ )
22
+ }
23
+
24
+ export const Plugin = GaProductListItemsBase
@@ -1,24 +1,16 @@
1
1
  import type { ProductItemsGridProps } from '@graphcommerce/magento-product'
2
2
  import { PluginProps } from '@graphcommerce/next-config'
3
- import { gtagSelectItem } from '../events/gtagSelectItem/gtagSelectItem'
4
- import { useGtagViewItemList } from '../events/useGtagViewItemList/useGtagViewItemList'
3
+ import { GoogleAnalyticsItemList } from '../components/GoogleAnalyticsItemList'
5
4
 
6
5
  export const component = 'ProductListItemsBase'
7
6
  export const exported = '@graphcommerce/magento-product'
8
7
 
9
8
  export function GaProductListItemsBase(props: PluginProps<ProductItemsGridProps>) {
10
- const { Prev, onClick, ...rest } = props
11
-
12
- useGtagViewItemList(props)
13
-
9
+ const { Prev, ...rest } = props
14
10
  return (
15
- <Prev
16
- {...rest}
17
- onClick={(e, item) => {
18
- gtagSelectItem({ item })
19
- return onClick?.(e, item)
20
- }}
21
- />
11
+ <GoogleAnalyticsItemList {...rest}>
12
+ <Prev {...rest} />
13
+ </GoogleAnalyticsItemList>
22
14
  )
23
15
  }
24
16
 
@@ -1,17 +0,0 @@
1
- import { ProductToGtagItemFragment } from '../productToGtagItem/ProductToGtagItem.gql'
2
- import { productToGtagItem } from '../productToGtagItem/productToGtagItem'
3
-
4
- type GtagSelectItemProps<T extends ProductToGtagItemFragment> = {
5
- item: T
6
- listId?: string
7
- title?: string
8
- }
9
-
10
- export function gtagSelectItem<T extends ProductToGtagItemFragment>(props: GtagSelectItemProps<T>) {
11
- const { item, listId: item_list_id, title: item_list_name } = props
12
- globalThis.gtag?.('event', 'select_item', {
13
- item_list_id,
14
- item_list_name,
15
- items: productToGtagItem(item),
16
- })
17
- }
@@ -1,36 +0,0 @@
1
- import { nonNullable, useMemoDeep } from '@graphcommerce/next-ui'
2
- import { useEffect } from 'react'
3
- import { ProductToGtagItemFragment } from '../productToGtagItem/ProductToGtagItem.gql'
4
- import { GtagItem, productToGtagItem } from '../productToGtagItem/productToGtagItem'
5
-
6
- export type UseGtagViewItemListProps<
7
- P extends ProductToGtagItemFragment = ProductToGtagItemFragment,
8
- > = {
9
- title: string
10
- items?: (P | null | undefined)[] | null
11
- listId?: string
12
- }
13
-
14
- export type ViewItemList = {
15
- item_list_id: string
16
- item_list_name: string
17
- items: GtagItem[]
18
- }
19
-
20
- export function useGtagViewItemList<P extends ProductToGtagItemFragment>(
21
- props: UseGtagViewItemListProps<P>,
22
- ) {
23
- const { title, items, listId } = props
24
-
25
- const eventData: ViewItemList = useMemoDeep(
26
- () => ({
27
- item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
28
- item_list_name: title,
29
- items:
30
- items?.map((item) => (item ? productToGtagItem(item) : null)).filter(nonNullable) ?? [],
31
- }),
32
- [listId, items, title],
33
- )
34
-
35
- useEffect(() => globalThis.gtag?.('event', 'view_item_list', eventData), [eventData])
36
- }