@graphcommerce/googleanalytics 2.0.6 → 3.0.0
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 +18 -0
- package/components/GoogleAnalyticsScript.tsx +1 -1
- package/events/GtagAddPaymentInfo.graphql +36 -0
- package/events/GtagAddShippingInfo.graphql +38 -0
- package/events/GtagAddToCart.graphql +10 -0
- package/events/GtagBeginCheckout.graphql +38 -0
- package/events/gtagAddPaymentInfo.ts +24 -0
- package/events/gtagAddShippingInfo.ts +24 -0
- package/events/gtagAddToCart.ts +25 -0
- package/events/gtagBeginCheckout.ts +24 -0
- package/events/gtagSelectItem.ts +20 -0
- package/events/useGtagPurchase.ts +28 -0
- package/events/useGtagViewItemList.ts +24 -0
- package/index.ts +7 -0
- package/package.json +12 -6
- package/types/types.ts +31 -0
- package/utils/index.ts +19 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#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.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 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)]:
|
|
12
|
+
- @graphcommerce/next-ui@4.28.1
|
|
13
|
+
- @graphcommerce/magento-product@4.7.3
|
|
14
|
+
|
|
15
|
+
## 2.0.7
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#1490](https://github.com/graphcommerce-org/graphcommerce/pull/1490) [`d311ef48b`](https://github.com/graphcommerce-org/graphcommerce/commit/d311ef48bb3e97806d992af5516d6b7f183ec9cb) Thanks [@paales](https://github.com/paales)! - upgraded packages
|
|
20
|
+
|
|
3
21
|
## 2.0.6
|
|
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,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,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 { GtagAddPaymentInfoFragment } from './GtagAddPaymentInfo.gql'
|
|
2
|
+
|
|
3
|
+
export function gtagAddPaymentInfo(cart?: GtagAddPaymentInfoFragment | null) {
|
|
4
|
+
if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
|
|
5
|
+
window.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,24 @@
|
|
|
1
|
+
import { GtagAddShippingInfoFragment } from './GtagAddShippingInfo.gql'
|
|
2
|
+
|
|
3
|
+
export const gtagAddShippingInfo = (cart?: GtagAddShippingInfoFragment | null) => {
|
|
4
|
+
if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
|
|
5
|
+
window.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,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,24 @@
|
|
|
1
|
+
import { GtagBeginCheckoutFragment } from './GtagBeginCheckout.gql'
|
|
2
|
+
|
|
3
|
+
export function gtagBeginCheckout(cart?: GtagBeginCheckoutFragment | null) {
|
|
4
|
+
if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
|
|
5
|
+
window.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,20 @@
|
|
|
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
|
+
}
|
|
@@ -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
|
+
// window.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,24 @@
|
|
|
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/index.ts
CHANGED
|
@@ -1 +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'
|
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
|
+
"version": "3.0.0",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -11,15 +11,21 @@
|
|
|
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
|
-
"@graphcommerce/eslint-config-pwa": "^4.1.
|
|
16
|
-
"@graphcommerce/prettier-config-pwa": "^4.0.
|
|
20
|
+
"@graphcommerce/eslint-config-pwa": "^4.1.8",
|
|
21
|
+
"@graphcommerce/prettier-config-pwa": "^4.0.6",
|
|
17
22
|
"@graphcommerce/typescript-config-pwa": "^4.0.2",
|
|
18
23
|
"@types/gtag.js": "^0.0.10"
|
|
19
24
|
},
|
|
20
25
|
"peerDependencies": {
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"react
|
|
26
|
+
"@mui/material": "5.10.0",
|
|
27
|
+
"next": "^12.1.2",
|
|
28
|
+
"react": "^18.0.0",
|
|
29
|
+
"react-dom": "^18.0.0"
|
|
24
30
|
}
|
|
25
31
|
}
|
package/types/types.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|