@graphcommerce/googleanalytics 2.0.7 → 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,23 @@
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
+
9
+ ## 3.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - [#1655](https://github.com/graphcommerce-org/graphcommerce/pull/1655) [`3dde492ad`](https://github.com/graphcommerce-org/graphcommerce/commit/3dde492ad3a49d96481eeb7453fb305d0017b1a5) Thanks [@FrankHarland](https://github.com/FrankHarland)! - Added Google Analytics support.
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [[`9e630670f`](https://github.com/graphcommerce-org/graphcommerce/commit/9e630670ff6c952ab7b938d890b5509804985cf3), [`2e9fa5984`](https://github.com/graphcommerce-org/graphcommerce/commit/2e9fa5984a07ff14fc1b3a4f62189a26e8e3ecdd), [`adf13069a`](https://github.com/graphcommerce-org/graphcommerce/commit/adf13069af6460c960276b402237371c12fc6dec), [`1b1504c9b`](https://github.com/graphcommerce-org/graphcommerce/commit/1b1504c9b0e51f2787bce91e1ff1940f540411d6)]:
18
+ - @graphcommerce/next-ui@4.28.1
19
+ - @graphcommerce/magento-product@4.7.3
20
+
3
21
  ## 2.0.7
4
22
 
5
23
  ### Patch Changes
@@ -18,7 +18,7 @@ export function GoogleAnalyticsScript() {
18
18
  window.dataLayer = window.dataLayer || [];
19
19
  function gtag(){dataLayer.push(arguments);}
20
20
  gtag('js', new Date());
21
- gtag('config', '${id}');
21
+ gtag('config', '${id}', { 'debug_mode':true });
22
22
  `}</Script>
23
23
  </>
24
24
  )
@@ -0,0 +1,36 @@
1
+ fragment GtagAddPaymentInfo on Cart
2
+ @inject(into: ["PaymentMethodContext", "PaymentMethodUpdated"]) {
3
+ items {
4
+ uid
5
+ prices {
6
+ price {
7
+ currency
8
+ value
9
+ }
10
+ discounts {
11
+ amount {
12
+ currency
13
+ value
14
+ }
15
+ }
16
+ }
17
+ quantity
18
+ product {
19
+ uid
20
+ name
21
+ sku
22
+ }
23
+ }
24
+ applied_coupons {
25
+ code
26
+ }
27
+ prices {
28
+ grand_total {
29
+ currency
30
+ value
31
+ }
32
+ }
33
+ selected_payment_method {
34
+ code
35
+ }
36
+ }
@@ -0,0 +1,24 @@
1
+ import { GtagAddPaymentInfoFragment } from './GtagAddPaymentInfo.gql'
2
+
3
+ export function gtagAddPaymentInfo<C extends GtagAddPaymentInfoFragment>(cart?: C | null) {
4
+ if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
+ globalThis.gtag?.('event', 'add_payment_info', {
6
+ currency: cart?.prices?.grand_total?.currency,
7
+ value: cart?.prices?.grand_total?.value,
8
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
9
+ payment_type: cart?.selected_payment_method?.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
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
16
+ (sum, discount) => sum + (discount?.amount?.value ?? 0),
17
+ 0,
18
+ ),
19
+ price: item?.prices?.price.value,
20
+ quantity: item?.quantity,
21
+ })),
22
+ })
23
+ }
24
+ }
@@ -0,0 +1,38 @@
1
+ fragment GtagAddShippingInfo on Cart @inject(into: ["ShippingPageFragment"]) {
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
+ }
@@ -0,0 +1,24 @@
1
+ import { GtagAddShippingInfoFragment } from './GtagAddShippingInfo.gql'
2
+
3
+ export function gtagAddShippingInfo<C extends GtagAddShippingInfoFragment>(cart?: C | null) {
4
+ if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
+ globalThis.gtag?.('event', 'add_shipping_info', {
6
+ currency: cart?.prices?.grand_total?.currency,
7
+ value: cart?.prices?.grand_total?.value,
8
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
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:
18
+ item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
19
+ price: item?.prices?.price.value,
20
+ quantity: item?.quantity,
21
+ })),
22
+ })
23
+ }
24
+ }
@@ -0,0 +1,10 @@
1
+ fragment GtagAddToCart on Cart @inject(into: ["CartItemCountChanged"]) {
2
+ items {
3
+ prices {
4
+ price {
5
+ currency
6
+ value
7
+ }
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,25 @@
1
+ import { FetchResult } from '@graphcommerce/graphql'
2
+ import {
3
+ AddProductsToCartMutation,
4
+ AddProductsToCartMutationVariables,
5
+ } from '@graphcommerce/magento-product'
6
+ import { GtagAddToCartFragment } from './GtagAddToCart.gql'
7
+
8
+ // @todo add types
9
+ // @todo some thing still needs to be done for configurables and bundles as you can have multiple items of the same SKU in your cart, but with different configurations/prices
10
+ // F.E. 'sock-red' has 2 variants, 'sock-red:small' (€ 5) and 'sock-red:large' (€ 7,50). At this time we dont know the value of the item added if multiple variants are added
11
+ export const gtagAddToCart = (
12
+ result: FetchResult<AddProductsToCartMutation>,
13
+ variables: AddProductsToCartMutationVariables,
14
+ ) => {
15
+ if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
16
+ const addedItem = result.data?.addProductsToCart?.cart.items?.slice(-1)[0]
17
+
18
+ // console.log(result.data?.addProductsToCart, variables)
19
+ // console.log({
20
+ // currency,
21
+ // value,
22
+ // items,
23
+ // })
24
+ }
25
+ }
@@ -0,0 +1,38 @@
1
+ fragment GtagBeginCheckout 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
+ }
@@ -0,0 +1,24 @@
1
+ import { GtagBeginCheckoutFragment } from './GtagBeginCheckout.gql'
2
+
3
+ export function gtagBeginCheckout<C extends GtagBeginCheckoutFragment>(cart?: C | null) {
4
+ if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
+ globalThis.gtag?.('event', 'begin_checkout', {
6
+ currency: cart?.prices?.grand_total?.currency,
7
+ value: cart?.prices?.grand_total?.value,
8
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
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:
18
+ item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
19
+ price: item?.prices?.price.value,
20
+ quantity: item?.quantity,
21
+ })),
22
+ })
23
+ }
24
+ }
@@ -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
+ }
@@ -0,0 +1,28 @@
1
+ export const useGtagPurchase = () => {
2
+ // const bla = useCartQuery(CartItemSummaryDocument)
3
+
4
+ if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
5
+ // console.log(cart, 'trigger gtagPurchase')
6
+ // globalThis.gtag?.('event', 'purchase', {
7
+ // currency: cart?.prices?.grand_total?.currency,
8
+ // // transaction_id:,
9
+ // // shipping:,
10
+ // // tax:,
11
+ // value: cart?.prices?.grand_total?.value,
12
+ // coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
13
+ // payment_type: cart?.selected_payment_method?.code,
14
+ // items: cart?.items?.map((item) => ({
15
+ // item_id: item?.product.sku,
16
+ // item_name: item?.product.name,
17
+ // currency: item?.prices?.price.currency,
18
+ // discount: item?.prices?.discounts?.reduce(
19
+ // // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
20
+ // (sum, discount) => sum + (discount?.amount?.value ?? 0),
21
+ // 0,
22
+ // ),
23
+ // price: item?.prices?.price.value,
24
+ // quantity: item?.quantity,
25
+ // })),
26
+ // })
27
+ }
28
+ }
@@ -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 +1,8 @@
1
1
  export * from './components'
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": "2.0.7",
5
+ "version": "3.0.1",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -11,6 +11,11 @@
11
11
  "project": "./tsconfig.json"
12
12
  }
13
13
  },
14
+ "dependencies": {
15
+ "@graphcommerce/graphql-mesh": "4.2.0",
16
+ "@graphcommerce/magento-product": "4.7.3",
17
+ "@graphcommerce/next-ui": "4.28.1"
18
+ },
14
19
  "devDependencies": {
15
20
  "@graphcommerce/eslint-config-pwa": "^4.1.8",
16
21
  "@graphcommerce/prettier-config-pwa": "^4.0.6",
@@ -18,6 +23,7 @@
18
23
  "@types/gtag.js": "^0.0.10"
19
24
  },
20
25
  "peerDependencies": {
26
+ "@mui/material": "5.10.0",
21
27
  "next": "^12.1.2",
22
28
  "react": "^18.0.0",
23
29
  "react-dom": "^18.0.0"