@graphcommerce/googleanalytics 3.0.0 → 3.0.1

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
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1676](https://github.com/graphcommerce-org/graphcommerce/pull/1676) [`55cabbbc5`](https://github.com/graphcommerce-org/graphcommerce/commit/55cabbbc517b35c5f909bd771e619a87614e4c97) Thanks [@paales](https://github.com/paales)! - Fix issue where gtag wouldn't exist
8
+
3
9
  ## 3.0.0
4
10
 
5
11
  ### Major Changes
@@ -1,8 +1,8 @@
1
1
  import { GtagAddPaymentInfoFragment } from './GtagAddPaymentInfo.gql'
2
2
 
3
- export function gtagAddPaymentInfo(cart?: GtagAddPaymentInfoFragment | null) {
3
+ export function gtagAddPaymentInfo<C extends GtagAddPaymentInfoFragment>(cart?: C | null) {
4
4
  if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
- window.gtag?.('event', 'add_payment_info', {
5
+ globalThis.gtag?.('event', 'add_payment_info', {
6
6
  currency: cart?.prices?.grand_total?.currency,
7
7
  value: cart?.prices?.grand_total?.value,
8
8
  coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
@@ -1,8 +1,8 @@
1
1
  import { GtagAddShippingInfoFragment } from './GtagAddShippingInfo.gql'
2
2
 
3
- export const gtagAddShippingInfo = (cart?: GtagAddShippingInfoFragment | null) => {
3
+ export function gtagAddShippingInfo<C extends GtagAddShippingInfoFragment>(cart?: C | null) {
4
4
  if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
- window.gtag?.('event', 'add_shipping_info', {
5
+ globalThis.gtag?.('event', 'add_shipping_info', {
6
6
  currency: cart?.prices?.grand_total?.currency,
7
7
  value: cart?.prices?.grand_total?.value,
8
8
  coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
@@ -1,8 +1,8 @@
1
1
  import { GtagBeginCheckoutFragment } from './GtagBeginCheckout.gql'
2
2
 
3
- export function gtagBeginCheckout(cart?: GtagBeginCheckoutFragment | null) {
3
+ export function gtagBeginCheckout<C extends GtagBeginCheckoutFragment>(cart?: C | null) {
4
4
  if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
- window.gtag?.('event', 'begin_checkout', {
5
+ globalThis.gtag?.('event', 'begin_checkout', {
6
6
  currency: cart?.prices?.grand_total?.currency,
7
7
  value: cart?.prices?.grand_total?.value,
8
8
  coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
@@ -0,0 +1,17 @@
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
+ }
@@ -0,0 +1,20 @@
1
+ fragment ProductToGtagItem on ProductInterface {
2
+ name
3
+ sku
4
+ price_range {
5
+ minimum_price {
6
+ final_price {
7
+ value
8
+ currency
9
+ }
10
+ discount {
11
+ amount_off
12
+ }
13
+ }
14
+ }
15
+ categories {
16
+ uid
17
+ url_key
18
+ name
19
+ }
20
+ }
@@ -0,0 +1,38 @@
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_name: item.name ?? '',
28
+ item_id: item.sku ?? '',
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
+ }
@@ -3,7 +3,7 @@ export const useGtagPurchase = () => {
3
3
 
4
4
  if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
5
  // console.log(cart, 'trigger gtagPurchase')
6
- // window.gtag?.('event', 'purchase', {
6
+ // globalThis.gtag?.('event', 'purchase', {
7
7
  // currency: cart?.prices?.grand_total?.currency,
8
8
  // // transaction_id:,
9
9
  // // shipping:,
@@ -0,0 +1,36 @@
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
+ }
package/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './components'
2
- export * from './events/useGtagViewItemList'
3
- export * from './events/gtagBeginCheckout'
4
- export * from './events/gtagAddPaymentInfo'
5
- export * from './events/gtagAddShippingInfo'
6
- export * from './events/gtagSelectItem'
7
- export * from './events/useGtagPurchase'
8
- export * from './events/gtagAddToCart'
2
+ export * from './events/useGtagViewItemList/useGtagViewItemList'
3
+ export * from './events/gtagBeginCheckout/gtagBeginCheckout'
4
+ export * from './events/gtagAddPaymentInfo/gtagAddPaymentInfo'
5
+ export * from './events/gtagAddShippingInfo/gtagAddShippingInfo'
6
+ export * from './events/gtagSelectItem/gtagSelectItem'
7
+ export * from './events/useGtagPurchase/useGtagPurchase'
8
+ 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": "3.0.0",
5
+ "version": "3.0.1",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -1,20 +0,0 @@
1
- import { ProductInterface } from '@graphcommerce/graphql-mesh'
2
- import { itemToEvent } from '../utils'
3
-
4
- type GtagSelectItemProps = {
5
- item: Partial<ProductInterface>
6
- listId?: string
7
- title?: string
8
- }
9
-
10
- export function gtagSelectItem({ item, listId, title }: GtagSelectItemProps) {
11
- gtag?.('event', 'select_item', {
12
- item_list_id: listId,
13
- item_list_name: title,
14
- items: itemToEvent({
15
- ...item,
16
- name: item.name ?? '',
17
- sku: item.sku ?? '',
18
- }),
19
- })
20
- }
@@ -1,24 +0,0 @@
1
- import { ProductListItemFragment } from '@graphcommerce/magento-product'
2
- import { filterNonNullableKeys } from '@graphcommerce/next-ui'
3
- import { useEffect, useMemo } from 'react'
4
- import { ViewItemList } from '../types/types'
5
- import { itemToEvent } from '../utils'
6
-
7
- export type UseGtagViewItemListProps = {
8
- title: string
9
- items?: (ProductListItemFragment | null | undefined)[] | null | undefined
10
- listId?: string
11
- }
12
-
13
- export function useGtagViewItemList({ title, items, listId }: UseGtagViewItemListProps) {
14
- const eventData: ViewItemList = useMemo(
15
- () => ({
16
- item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'),
17
- item_list_name: title,
18
- items: filterNonNullableKeys(items, ['name', 'sku']).map((item) => itemToEvent(item)),
19
- }),
20
- [listId, items, title],
21
- )
22
-
23
- useEffect(() => gtag?.('event', 'view_item_list', eventData), [eventData])
24
- }
package/types/types.ts DELETED
@@ -1,31 +0,0 @@
1
- import type { ProductInterface } from '@graphcommerce/graphql-mesh'
2
- import { RequiredKeys } from '@graphcommerce/next-ui'
3
-
4
- export type Item = {
5
- item_id: string
6
- item_name: string
7
- affiliation?: string
8
- coupon?: string
9
- currency?: string
10
- discount?: number
11
- index?: number
12
- item_brand?: string
13
- item_category?: string
14
- item_category2?: string
15
- item_category3?: string
16
- item_category4?: string
17
- item_category5?: string
18
- item_list_id?: string
19
- item_list_name?: string
20
- item_variant?: string
21
- location_id?: string
22
- price?: number
23
- quantity?: number
24
- }
25
- export type ViewItemList = {
26
- item_list_id: string
27
- item_list_name: string
28
- items: Item[]
29
- }
30
-
31
- export type ProductItem = RequiredKeys<Partial<ProductInterface>, 'name' | 'sku'>
package/utils/index.ts DELETED
@@ -1,19 +0,0 @@
1
- import { filterNonNullableKeys } from '@graphcommerce/next-ui'
2
- import { Item, ProductItem } from '../types/types'
3
-
4
- export function itemToEvent(item: ProductItem): Item {
5
- const categories = filterNonNullableKeys(item.categories, ['include_in_menu', 'name'])
6
-
7
- return {
8
- item_name: item.name,
9
- item_id: item.sku,
10
- price: item.price_range?.minimum_price.final_price.value ?? undefined,
11
- currency: item.price_range?.minimum_price.final_price.currency ?? undefined,
12
- discount: item.price_range?.minimum_price.discount?.amount_off ?? undefined,
13
- item_category: categories[0]?.name,
14
- item_category2: categories[1]?.name,
15
- item_category3: categories[2]?.name,
16
- item_category4: categories[3]?.name,
17
- item_category5: categories[4]?.name,
18
- }
19
- }