-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2158 from graphcommerce-org/feature/GCOM-1312
Refactor GA4 / GTM tracking preferences (GCOM-1312)
- Loading branch information
Showing
73 changed files
with
1,502 additions
and
842 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@graphcommerce/google-datalayer": patch | ||
"@graphcommerce/googletagmanager": patch | ||
"@graphcommerce/googleanalytics": patch | ||
--- | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
extend input GraphCommerceConfig { | ||
analytics: AnalyticsConfig | ||
} | ||
|
||
""" | ||
EventFormat is an enumatation of different event formats. This decides what the format of the event data will be. | ||
""" | ||
enum EventFormat { | ||
GA3 | ||
GA4 | ||
} | ||
|
||
""" | ||
AnalyticsConfig will contain all configuration values for the analytics in GraphCommerce. | ||
""" | ||
input AnalyticsConfig { | ||
""" | ||
eventFormat contains the list of fired and formatted events | ||
""" | ||
eventFormat: [EventFormat!] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# @graphcommerce/google-datalayer | ||
|
||
This package contains utilities that can be used in different app analytics | ||
trackers. | ||
|
||
add_payment_info add_shipping_info |
63 changes: 63 additions & 0 deletions
63
packages/google-datalayer/components/AnalyticsItemList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { nonNullable, useMemoObject } from '@graphcommerce/next-ui' | ||
import { useEventCallback } from '@mui/material' | ||
import React, { useContext, useEffect, useMemo } from 'react' | ||
import { Item, ProductToItemFragment, productToItem } from '../lib' | ||
import { event } from '../lib/event' | ||
|
||
export type UseViewItemListProps<P extends ProductToItemFragment = ProductToItemFragment> = { | ||
title: string | ||
items?: (P | null | undefined)[] | null | ||
listId?: string | ||
} | ||
|
||
export type ViewItemList = { | ||
item_list_id: string | ||
item_list_name: string | ||
items: Item[] | ||
} | ||
|
||
const GoogleTagManagerItemListContext = React.createContext<{ | ||
item_list_id: string | ||
item_list_name: string | ||
}>({ item_list_id: '', item_list_name: '' }) | ||
|
||
export function useListItemHandler(item: ProductToItemFragment) { | ||
const { item_list_id, item_list_name } = useContext(GoogleTagManagerItemListContext) | ||
return useEventCallback(() => | ||
event('select_item', { | ||
item_list_id, | ||
item_list_name, | ||
items: productToItem(item), | ||
}), | ||
) | ||
} | ||
|
||
export function ItemList<P extends ProductToItemFragment = ProductToItemFragment>( | ||
props: UseViewItemListProps<P> & { children: React.ReactNode }, | ||
) { | ||
const { title, items, listId, children } = props | ||
|
||
const eventData: ViewItemList = useMemoObject({ | ||
item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'), | ||
item_list_name: title, | ||
items: items?.map((item) => (item ? productToItem(item) : null)).filter(nonNullable) ?? [], | ||
}) | ||
|
||
useEffect(() => { | ||
event('view_item_list', eventData) | ||
}, [eventData]) | ||
|
||
const value = useMemo( | ||
() => ({ | ||
item_list_id: listId ?? title?.toLowerCase().replace(/\s/g, '_'), | ||
item_list_name: title ?? listId, | ||
}), | ||
[listId, title], | ||
) | ||
|
||
return ( | ||
<GoogleTagManagerItemListContext.Provider value={value}> | ||
{children} | ||
</GoogleTagManagerItemListContext.Provider> | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
...AddPaymentInfo/GtagAddPaymentInfo.graphql → ...yment_info/AddPaymentInfoFragment.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './addPaymentInfo' | ||
export * from './AddPaymentInfoFragment.gql' |
2 changes: 1 addition & 1 deletion
2
...dShippingInfo/GtagAddShippingInfo.graphql → ...ing_info/AddSchippingInfoFragment.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './addShippingInfo' | ||
export * from './AddSchippingInfoFragment.gql' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { FetchResult } from '@graphcommerce/graphql' | ||
import { | ||
AddProductsToCartFields, | ||
AddProductsToCartMutation, | ||
findAddedItems, | ||
toUserErrors, | ||
} from '@graphcommerce/magento-product' | ||
import { nonNullable } from '@graphcommerce/next-ui' | ||
import { event } from '../../lib/event' | ||
|
||
export const addToCart = ( | ||
result: FetchResult<AddProductsToCartMutation>, | ||
variables: AddProductsToCartFields, | ||
) => { | ||
const { data, errors } = result | ||
const cart = data?.addProductsToCart?.cart | ||
|
||
const addedItems = findAddedItems(data, variables) | ||
|
||
const items = addedItems | ||
.map(({ itemVariable, itemInCart }) => { | ||
if (!itemInCart) return null | ||
const { product, prices } = itemInCart | ||
return { | ||
item_id: product.sku, | ||
item_name: product.name, | ||
currency: prices?.price.currency, | ||
price: (prices?.row_total_including_tax.value ?? 1) / itemInCart.quantity, | ||
quantity: itemVariable.quantity, | ||
discount: prices?.discounts?.reduce( | ||
(sum, discount) => sum + (discount?.amount?.value ?? 0) / itemVariable.quantity, | ||
0, | ||
), | ||
} | ||
}) | ||
.filter(nonNullable) | ||
|
||
const userErrors = toUserErrors(result.data) | ||
if ((errors && errors.length > 0) || userErrors.length > 0) { | ||
event('add_to_cart_error', { | ||
userErrors: userErrors?.map((e) => e.message), | ||
errors: errors?.map((e) => e.message), | ||
variables, | ||
}) | ||
} | ||
|
||
if (!items.length) return | ||
|
||
event('add_to_cart', { | ||
currency: cart?.prices?.grand_total?.currency, | ||
value: addedItems.reduce( | ||
(sum, { itemVariable, itemInCart }) => | ||
sum + (itemInCart?.prices?.row_total_including_tax.value ?? 1) / itemVariable.quantity, | ||
0, | ||
), | ||
items, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './addToCart' | ||
export * from './AddToCartFragment.gql' |
2 changes: 1 addition & 1 deletion
2
...agBeginCheckout/GtagBeginCheckout.graphql → ...in_checkout/BeginCheckoutFragment.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './beginCheckout' | ||
export * from './BeginCheckoutFragment.gql' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './purchase' |
14 changes: 7 additions & 7 deletions
14
...tagAddPurchaseInfo/gtagAddPurchaseInfo.ts → ...gle-datalayer/events/purchase/purchase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
import { PaymentMethodContextFragment } from '@graphcommerce/magento-cart-payment-method/Api/PaymentMethodContext.gql' | ||
import { event } from '../../lib/event' | ||
|
||
export function gtagAddPurchaseInfo( | ||
export const purchase = <C extends PaymentMethodContextFragment>( | ||
orderNumber: string, | ||
cart: PaymentMethodContextFragment | null | undefined, | ||
) { | ||
globalThis.gtag?.('event', 'purchase', { | ||
cart: C | null | undefined, | ||
) => | ||
event('purchase', { | ||
...cart, | ||
transaction_id: orderNumber, | ||
currency: cart?.prices?.grand_total?.currency, | ||
value: cart?.prices?.grand_total?.value, | ||
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code), | ||
coupon: cart?.applied_coupons?.map((coupon) => coupon?.code).join(' '), | ||
payment_type: cart?.selected_payment_method?.code, | ||
tax: cart?.prices?.applied_taxes?.reduce((sum, tax) => sum + (tax?.amount?.value ?? 0), 0), | ||
items: cart?.items?.map((item) => ({ | ||
item_id: item?.product.sku, | ||
item_name: item?.product.name, | ||
currency: item?.prices?.price.currency, | ||
discount: item?.prices?.discounts?.reduce( | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
(sum, discount) => sum + (discount?.amount?.value ?? 0), | ||
0, | ||
), | ||
price: item?.prices?.price.value, | ||
quantity: item?.quantity, | ||
})), | ||
}) | ||
} |
2 changes: 1 addition & 1 deletion
2
...RemoveFromCart/GtagRemoveFromCart.graphql → ..._from_cart/RemoveFromCartFragment.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './removeFromCart' | ||
export * from './RemoveFromCartFragment.gql' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './select_item' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { event } from '../../lib/event' | ||
|
||
export const selectItem = (itemListId: string, itemListName: string, items: unknown[]) => | ||
event('select_item', { | ||
item_list_id: itemListId, | ||
item_list_name: itemListName, | ||
items, | ||
}) |
2 changes: 1 addition & 1 deletion
2
.../events/gtagViewCart/GtagViewCart.graphql → ...events/view_cart/ViewCartFragment.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './viewCart' | ||
export * from './ViewCartFragment.gql' |
Oops, something went wrong.