@graphcommerce/google-datalayer 8.0.5-canary.0 → 8.0.5-canary.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/Config.graphqls +5 -13
  3. package/README.md +2 -3
  4. package/api/sendEvent.ts +50 -0
  5. package/components/DatalayerViewItemList.tsx +45 -0
  6. package/index.ts +1 -0
  7. package/mapping/cartItemToDatalayerItem/CartItem_DatalayerItem.graphql +31 -0
  8. package/mapping/cartItemToDatalayerItem/cartItemToDatalayerItem.ts +27 -0
  9. package/mapping/cartItemToRemoveFromCart/CartItem_RemoveFromCart.graphql +3 -0
  10. package/mapping/cartItemToRemoveFromCart/cartToRemoveFromCart.ts +13 -0
  11. package/mapping/cartToAddPaymentInfo/Cart_AddPaymentInfo.graphql +10 -0
  12. package/mapping/cartToAddPaymentInfo/cartToAddPaymentInfo.ts +10 -0
  13. package/mapping/cartToAddShippingInfo/Cart_AddShippingInfo.graphql +12 -0
  14. package/mapping/cartToAddShippingInfo/cartToAddShippingInfo.ts +15 -0
  15. package/mapping/cartToBeginCheckout/Cart_BeginCheckout.graphql +6 -0
  16. package/mapping/cartToBeginCheckout/cartToBeginCheckout.ts +9 -0
  17. package/mapping/cartToDatalayerItems/Cart_DatalayerItems.graphql +11 -0
  18. package/mapping/cartToDatalayerItems/cartToDatalayerItems.ts +10 -0
  19. package/mapping/cartToViewCart/Cart_ViewCart.graphql +4 -0
  20. package/mapping/cartToViewCart/cartToViewCart.ts +6 -0
  21. package/mapping/datalayerItemsToCurrencyValue/datalayerItemsToCurrencyValue.ts +8 -0
  22. package/mapping/orderToPurchase/orderToPurchase.ts +17 -0
  23. package/mapping/productItemsToViewItemList/productItemsToViewItemList.ts +22 -0
  24. package/{lib/productToItem/ProductToItem.graphql → mapping/productToDatalayerItem/Product_DatalayerItem.graphql} +3 -6
  25. package/{lib/productToItem/productToItem.ts → mapping/productToDatalayerItem/productToDatalayerItem.ts} +17 -9
  26. package/mapping/productToViewItem/Product_ViewItem.graphql +3 -0
  27. package/mapping/productToViewItem/productToViewItem.ts +11 -0
  28. package/package.json +12 -11
  29. package/plugins/GoogleDatalayerAddProductsToCartForm.tsx +33 -4
  30. package/plugins/GoogleDatalayerCartStartCheckout.tsx +17 -3
  31. package/plugins/GoogleDatalayerCartStartCheckoutLinkOrButton.tsx +4 -14
  32. package/plugins/GoogleDatalayerPaymentMethodButton.tsx +6 -4
  33. package/plugins/GoogleDatalayerPaymentMethodContextProvider.tsx +4 -2
  34. package/plugins/GoogleDatalayerProductListItem.tsx +7 -8
  35. package/plugins/GoogleDatalayerProductListItemsBase.tsx +4 -4
  36. package/plugins/GoogleDatalayerRemoveItemFromCart.tsx +13 -10
  37. package/plugins/GoogleDatalayerRemoveItemFromCartFab.tsx +9 -8
  38. package/plugins/GoogleDatalayerShippingMethodForm.tsx +10 -4
  39. package/plugins/GoogleDatalayerUpdateItemQuantity.tsx +15 -32
  40. package/plugins/GoogleDatalayerViewItem.tsx +5 -13
  41. package/plugins/GoogleDatalayerWebVitals.tsx +40 -0
  42. package/components/AnalyticsItemList.tsx +0 -63
  43. package/events/add_payment_info/AddPaymentInfoFragment.graphql +0 -36
  44. package/events/add_payment_info/addPaymentInfo.ts +0 -23
  45. package/events/add_payment_info/index.ts +0 -2
  46. package/events/add_shipping_info/AddSchippingInfoFragment.graphql +0 -38
  47. package/events/add_shipping_info/addShippingInfo.ts +0 -22
  48. package/events/add_shipping_info/index.ts +0 -2
  49. package/events/add_to_cart/AddToCartFragment.graphql +0 -30
  50. package/events/add_to_cart/addToCart.ts +0 -58
  51. package/events/add_to_cart/index.ts +0 -2
  52. package/events/begin_checkout/BeginCheckoutFragment.graphql +0 -38
  53. package/events/begin_checkout/beginCheckout.ts +0 -22
  54. package/events/begin_checkout/index.ts +0 -2
  55. package/events/purchase/index.ts +0 -1
  56. package/events/purchase/purchase.ts +0 -27
  57. package/events/remove_from_cart/RemoveFromCartFragment.graphql +0 -36
  58. package/events/remove_from_cart/index.ts +0 -2
  59. package/events/remove_from_cart/removeFromCart.ts +0 -21
  60. package/events/select_item/index.ts +0 -1
  61. package/events/select_item/select_item.ts +0 -8
  62. package/events/view_cart/ViewCartFragment.graphql +0 -35
  63. package/events/view_cart/index.ts +0 -2
  64. package/events/view_cart/viewCart.ts +0 -21
  65. package/events/view_item/index.ts +0 -1
  66. package/events/view_item/view_item.ts +0 -8
  67. package/events/view_item_list/index.ts +0 -1
  68. package/events/view_item_list/view_item_list.ts +0 -8
  69. package/lib/event.ts +0 -39
  70. package/lib/index.ts +0 -2
  71. package/plugins/GoogleDatalayerAllPagesPageview.tsx +0 -37
@@ -1,38 +0,0 @@
1
- fragment BeginCheckoutFragment on Cart @inject(into: ["CartStartCheckout"]) {
2
- prices {
3
- grand_total {
4
- currency
5
- value
6
- }
7
- }
8
- applied_coupons {
9
- code
10
- }
11
- items {
12
- __typename
13
- uid
14
- product {
15
- uid
16
- sku
17
- name
18
- }
19
- prices {
20
- price {
21
- value
22
- currency
23
- }
24
- discounts {
25
- amount {
26
- value
27
- }
28
- }
29
- }
30
- quantity
31
- ... on ConfigurableCartItem {
32
- configured_variant {
33
- uid
34
- sku
35
- }
36
- }
37
- }
38
- }
@@ -1,22 +0,0 @@
1
- import { event } from '../../lib/event'
2
- import { BeginCheckoutFragment } from './BeginCheckoutFragment.gql'
3
-
4
- export const beginCheckout = <C extends BeginCheckoutFragment>(cart?: C | null) =>
5
- event('begin_checkout', {
6
- ...cart,
7
- currency: cart?.prices?.grand_total?.currency,
8
- value: cart?.prices?.grand_total?.value,
9
- coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
10
- items: cart?.items?.map((item) => ({
11
- item_id: item?.product.sku,
12
- item_name: item?.product.name,
13
- currency: item?.prices?.price.currency,
14
- discount: item?.prices?.discounts?.reduce(
15
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
16
- 0,
17
- ),
18
- item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
19
- price: item?.prices?.price.value,
20
- quantity: item?.quantity,
21
- })),
22
- })
@@ -1,2 +0,0 @@
1
- export * from './beginCheckout'
2
- export * from './BeginCheckoutFragment.gql'
@@ -1 +0,0 @@
1
- export * from './purchase'
@@ -1,27 +0,0 @@
1
- import { PaymentMethodContextFragment } from '@graphcommerce/magento-cart-payment-method/Api/PaymentMethodContext.gql'
2
- import { event } from '../../lib/event'
3
-
4
- export const purchase = <C extends PaymentMethodContextFragment>(
5
- orderNumber: string,
6
- cart: C | null | undefined,
7
- ) =>
8
- event('purchase', {
9
- ...cart,
10
- transaction_id: orderNumber,
11
- currency: cart?.prices?.grand_total?.currency,
12
- value: cart?.prices?.grand_total?.value,
13
- coupon: cart?.applied_coupons?.map((coupon) => coupon?.code).join(' '),
14
- payment_type: cart?.selected_payment_method?.code,
15
- tax: cart?.prices?.applied_taxes?.reduce((sum, tax) => sum + (tax?.amount?.value ?? 0), 0),
16
- items: cart?.items?.map((item) => ({
17
- item_id: item?.product.sku,
18
- item_name: item?.product.name,
19
- currency: item?.prices?.price.currency,
20
- discount: item?.prices?.discounts?.reduce(
21
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
22
- 0,
23
- ),
24
- price: item?.prices?.price.value,
25
- quantity: item?.quantity,
26
- })),
27
- })
@@ -1,36 +0,0 @@
1
- fragment RemoveFromCartFragment on Cart @inject(into: ["CartItemCountChanged"]) {
2
- __typename
3
- prices {
4
- grand_total {
5
- currency
6
- value
7
- }
8
- }
9
- items {
10
- __typename
11
- uid
12
- product {
13
- uid
14
- sku
15
- name
16
- }
17
- prices {
18
- price {
19
- value
20
- currency
21
- }
22
- discounts {
23
- amount {
24
- value
25
- }
26
- }
27
- }
28
- quantity
29
- ... on ConfigurableCartItem {
30
- configured_variant {
31
- uid
32
- sku
33
- }
34
- }
35
- }
36
- }
@@ -1,2 +0,0 @@
1
- export * from './removeFromCart'
2
- export * from './RemoveFromCartFragment.gql'
@@ -1,21 +0,0 @@
1
- import { event } from '../../lib/event'
2
- import { RemoveFromCartFragment } from './RemoveFromCartFragment.gql'
3
-
4
- export const removeFromCart = <C extends RemoveFromCartFragment>(cart: C | null | undefined) =>
5
- event('remove_from_cart', {
6
- ...cart,
7
- currency: cart?.prices?.grand_total?.currency,
8
- value: cart?.prices?.grand_total?.value,
9
- items: cart?.items?.map((item) => ({
10
- item_id: item?.product.sku,
11
- item_name: item?.product.name,
12
- currency: item?.prices?.price.currency,
13
- discount: item?.prices?.discounts?.reduce(
14
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
15
- 0,
16
- ),
17
- item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
18
- price: item?.prices?.price.value,
19
- quantity: item?.quantity,
20
- })),
21
- })
@@ -1 +0,0 @@
1
- export * from './select_item'
@@ -1,8 +0,0 @@
1
- import { event } from '../../lib/event'
2
-
3
- export const selectItem = (itemListId: string, itemListName: string, items: unknown[]) =>
4
- event('select_item', {
5
- item_list_id: itemListId,
6
- item_list_name: itemListName,
7
- items,
8
- })
@@ -1,35 +0,0 @@
1
- fragment ViewCartFragment on Cart @inject(into: ["CartItemCountChanged"]) {
2
- prices {
3
- grand_total {
4
- currency
5
- value
6
- }
7
- }
8
- items {
9
- __typename
10
- uid
11
- product {
12
- uid
13
- sku
14
- name
15
- }
16
- prices {
17
- price {
18
- value
19
- currency
20
- }
21
- discounts {
22
- amount {
23
- value
24
- }
25
- }
26
- }
27
- quantity
28
- ... on ConfigurableCartItem {
29
- configured_variant {
30
- uid
31
- sku
32
- }
33
- }
34
- }
35
- }
@@ -1,2 +0,0 @@
1
- export * from './viewCart'
2
- export * from './ViewCartFragment.gql'
@@ -1,21 +0,0 @@
1
- import { event } from '../../lib/event'
2
- import { ViewCartFragment } from './ViewCartFragment.gql'
3
-
4
- export const viewCart = <C extends ViewCartFragment>(cart: C | null | undefined) =>
5
- event('view_cart', {
6
- ...cart,
7
- currency: cart?.prices?.grand_total?.currency,
8
- value: cart?.prices?.grand_total?.value,
9
- items: cart?.items?.map((item) => ({
10
- item_id: item?.product.sku,
11
- item_name: item?.product.name,
12
- currency: item?.prices?.price.currency,
13
- discount: item?.prices?.discounts?.reduce(
14
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
15
- 0,
16
- ),
17
- item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
18
- price: item?.prices?.price.value,
19
- quantity: item?.quantity,
20
- })),
21
- })
@@ -1 +0,0 @@
1
- export * from './view_item'
@@ -1,8 +0,0 @@
1
- import { event } from '../../lib/event'
2
-
3
- export const viewItem = (itemListId: string, itemListName: string, items: unknown[]) =>
4
- event('view_item', {
5
- item_list_id: itemListId,
6
- item_list_name: itemListName,
7
- items,
8
- })
@@ -1 +0,0 @@
1
- export * from './view_item_list'
@@ -1,8 +0,0 @@
1
- import { event } from '../../lib/event'
2
-
3
- export const viewItemList = (itemListId: string, itemListName: string, items: unknown[]) =>
4
- event('view_item_list', {
5
- item_list_id: itemListId,
6
- item_list_name: itemListName,
7
- items,
8
- })
package/lib/event.ts DELETED
@@ -1,39 +0,0 @@
1
- import { EventFormatSchema } from '@graphcommerce/next-config'
2
-
3
- type EventMapFunctionType = (
4
- eventName: Gtag.EventNames | (string & Record<never, never>),
5
- eventData: {
6
- [key: string]: unknown
7
- },
8
- ) => void
9
-
10
- type EventType = keyof (typeof EventFormatSchema)['Enum']
11
-
12
- const eventMap: { [key in EventType]: EventMapFunctionType } = {
13
- GA4: (eventName, eventData) => {
14
- if (import.meta.graphCommerce.googleAnalyticsId) {
15
- globalThis.gtag('event', eventName, eventData)
16
- }
17
-
18
- if (import.meta.graphCommerce.googleTagmanagerId) {
19
- globalThis.dataLayer.push({ event: eventName, ...eventData })
20
- }
21
- },
22
- GA3: (eventName, eventData) => {
23
- if (import.meta.graphCommerce.googleAnalyticsId) {
24
- console.warn(
25
- "Google Analytics 3 format is not supported for Google Analytics 4. Please update your event format to 'GA4'.",
26
- )
27
- }
28
-
29
- if (import.meta.graphCommerce.googleTagmanagerId) {
30
- globalThis.dataLayer.push({ event: eventName, ecommerce: eventData })
31
- }
32
- },
33
- }
34
-
35
- const eventsToBeFired = import.meta.graphCommerce.analytics?.eventFormat ?? ['GA4']
36
-
37
- export const event: EventMapFunctionType = (eventName, eventData) => {
38
- eventsToBeFired.map((e) => eventMap[e](eventName, eventData))
39
- }
package/lib/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './productToItem/productToItem'
2
- export * from './productToItem/ProductToItem.gql'
@@ -1,37 +0,0 @@
1
- import type { PagesProps } from '@graphcommerce/framer-next-pages'
2
- import type { PluginProps } from '@graphcommerce/next-config'
3
- import { useRouter } from 'next/router'
4
- import { useEffect } from 'react'
5
- import { event } from '../lib/event'
6
-
7
- export const component = 'FramerNextPages'
8
- export const exported = '@graphcommerce/framer-next-pages'
9
-
10
- function GoogleDatalayerAllPagesPageview(props: PluginProps<PagesProps>) {
11
- const { Prev, ...rest } = props
12
-
13
- const { events } = useRouter()
14
-
15
- useEffect(() => {
16
- const onRouteChangeComplete = (url: string) => {
17
- /**
18
- * Todo: the actual page_view event is currently disabled, because we run the risk of double counting page views.
19
- * https://developers.google.com/analytics/devguides/collection/ga4/views?client_type=gtag#manually_send_page_view_events
20
- *
21
- * https://developers.google.com/analytics/devguides/collection/ga4/single-page-applications?implementation=event
22
- */
23
- // event('page_view', {
24
- // page_title: '<Page Title>',
25
- // page_location: '<Page Location>',
26
- // })
27
- // event('pageview', { page: url })
28
- }
29
-
30
- events.on('routeChangeComplete', onRouteChangeComplete)
31
- return () => events.off('routeChangeComplete', onRouteChangeComplete)
32
- }, [events])
33
-
34
- return <Prev {...rest} />
35
- }
36
-
37
- export const Plugin = GoogleDatalayerAllPagesPageview