@graphcommerce/googleanalytics 6.0.0-canary.37 → 6.0.0-canary.39

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,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 6.0.0-canary.39
4
+
5
+ ## 6.0.0-canary.38
6
+
7
+ ### Patch Changes
8
+
9
+ - [#1835](https://github.com/graphcommerce-org/graphcommerce/pull/1835) [`393d62cf1`](https://github.com/graphcommerce-org/graphcommerce/commit/393d62cf1833a61eb98777b7eab91ea891668e57) - Analytics do not rely on env variable but use googleAnalyticsId config ([@paales](https://github.com/paales))
10
+
3
11
  ## 6.0.0-canary.37
4
12
 
5
13
  ## 6.0.0-canary.36
@@ -1,19 +1,8 @@
1
- import { useRouter } from 'next/router'
1
+ import { useI18nConfig } from '@graphcommerce/next-ui'
2
2
  import Script from 'next/script'
3
3
 
4
4
  export function GoogleAnalyticsScript() {
5
- const gaEnv = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS
6
- const { locale } = useRouter()
7
- let id = ''
8
-
9
- if (!gaEnv) return null
10
-
11
- if (gaEnv.startsWith('{') && locale) {
12
- const GAConfig = JSON.parse(gaEnv)
13
- id = GAConfig[locale]
14
- } else {
15
- id = gaEnv
16
- }
5
+ const id = useI18nConfig().googleAnalyticsId ?? import.meta.graphCommerce.googleAnalyticsId
17
6
 
18
7
  return (
19
8
  <>
@@ -1,24 +1,22 @@
1
1
  import { GtagAddPaymentInfoFragment } from './GtagAddPaymentInfo.gql'
2
2
 
3
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
- }
4
+ globalThis.gtag?.('event', 'add_payment_info', {
5
+ currency: cart?.prices?.grand_total?.currency,
6
+ value: cart?.prices?.grand_total?.value,
7
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
8
+ payment_type: cart?.selected_payment_method?.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
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
15
+ (sum, discount) => sum + (discount?.amount?.value ?? 0),
16
+ 0,
17
+ ),
18
+ price: item?.prices?.price.value,
19
+ quantity: item?.quantity,
20
+ })),
21
+ })
24
22
  }
@@ -4,26 +4,24 @@ export function gtagAddPurchaseInfo(
4
4
  orderNumber: string,
5
5
  cart: PaymentMethodContextFragment | null | undefined,
6
6
  ) {
7
- if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
8
- globalThis.gtag?.('event', 'purchase', {
9
- transaction_id: orderNumber,
10
- currency: cart?.prices?.grand_total?.currency,
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
- tax: cart?.prices?.applied_taxes?.reduce((sum, tax) => sum + (tax?.amount?.value ?? 0), 0),
15
- items: cart?.items?.map((item) => ({
16
- item_id: item?.product.sku,
17
- item_name: item?.product.name,
18
- currency: item?.prices?.price.currency,
19
- discount: item?.prices?.discounts?.reduce(
20
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
21
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
22
- 0,
23
- ),
24
- price: item?.prices?.price.value,
25
- quantity: item?.quantity,
26
- })),
27
- })
28
- }
7
+ globalThis.gtag?.('event', 'purchase', {
8
+ transaction_id: orderNumber,
9
+ currency: cart?.prices?.grand_total?.currency,
10
+ value: cart?.prices?.grand_total?.value,
11
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
12
+ payment_type: cart?.selected_payment_method?.code,
13
+ tax: cart?.prices?.applied_taxes?.reduce((sum, tax) => sum + (tax?.amount?.value ?? 0), 0),
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
+ })
29
27
  }
@@ -8,33 +8,30 @@ export const gtagAddToCart = (
8
8
  result: FetchResult<AddProductsToCartMutation>,
9
9
  variables: AddProductsToCartMutationVariables,
10
10
  ) => {
11
- if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
12
- const addedItem = result.data?.addProductsToCart?.cart.items?.slice(-1)[0]
11
+ const addedItem = result.data?.addProductsToCart?.cart.items?.slice(-1)[0]
13
12
 
14
- if (addedItem && addedItem.prices && addedItem.prices.row_total_including_tax.value) {
15
- // we need to manually calculate pricePerItemInclTax (https://github.com/magento/magento2/issues/33848)
16
- const pricePerItemInclTax =
17
- addedItem.prices.row_total_including_tax.value / addedItem.quantity
18
- const addToCartValue = pricePerItemInclTax * variables.cartItems[0].quantity
13
+ if (addedItem && addedItem.prices && addedItem.prices.row_total_including_tax.value) {
14
+ // we need to manually calculate pricePerItemInclTax (https://github.com/magento/magento2/issues/33848)
15
+ const pricePerItemInclTax = addedItem.prices.row_total_including_tax.value / addedItem.quantity
16
+ const addToCartValue = pricePerItemInclTax * variables.cartItems[0].quantity
19
17
 
20
- globalThis.gtag?.('event', 'add_to_cart', {
21
- currency: addedItem?.prices?.price.currency,
22
- value: addToCartValue,
23
- items: [
24
- {
25
- item_id: addedItem?.product.sku,
26
- item_name: addedItem?.product.name,
27
- currency: addedItem?.prices?.price.currency,
28
- price: pricePerItemInclTax,
29
- quantity: variables.cartItems[0].quantity,
30
- discount: addedItem?.prices?.discounts?.reduce(
31
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
32
- (sum, discount) => sum + (discount?.amount?.value ?? 0),
33
- 0,
34
- ),
35
- },
36
- ],
37
- })
38
- }
18
+ globalThis.gtag?.('event', 'add_to_cart', {
19
+ currency: addedItem?.prices?.price.currency,
20
+ value: addToCartValue,
21
+ items: [
22
+ {
23
+ item_id: addedItem?.product.sku,
24
+ item_name: addedItem?.product.name,
25
+ currency: addedItem?.prices?.price.currency,
26
+ price: pricePerItemInclTax,
27
+ quantity: variables.cartItems[0].quantity,
28
+ discount: addedItem?.prices?.discounts?.reduce(
29
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
30
+ (sum, discount) => sum + (discount?.amount?.value ?? 0),
31
+ 0,
32
+ ),
33
+ },
34
+ ],
35
+ })
39
36
  }
40
37
  }
@@ -1,24 +1,21 @@
1
1
  import { GtagBeginCheckoutFragment } from './GtagBeginCheckout.gql'
2
2
 
3
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
- }
4
+ globalThis.gtag?.('event', 'begin_checkout', {
5
+ currency: cart?.prices?.grand_total?.currency,
6
+ value: cart?.prices?.grand_total?.value,
7
+ coupon: cart?.applied_coupons?.map((coupon) => coupon?.code),
8
+ items: cart?.items?.map((item) => ({
9
+ item_id: item?.product.sku,
10
+ item_name: item?.product.name,
11
+ currency: item?.prices?.price.currency,
12
+ discount: item?.prices?.discounts?.reduce(
13
+ (sum, discount) => sum + (discount?.amount?.value ?? 0),
14
+ 0,
15
+ ),
16
+ item_variant: item?.__typename === 'ConfigurableCartItem' ? item.configured_variant.sku : '',
17
+ price: item?.prices?.price.value,
18
+ quantity: item?.quantity,
19
+ })),
20
+ })
24
21
  }
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": "6.0.0-canary.37",
5
+ "version": "6.0.0-canary.39",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,18 +12,18 @@
12
12
  }
13
13
  },
14
14
  "dependencies": {
15
- "@graphcommerce/graphql-mesh": "6.0.0-canary.37",
16
- "@graphcommerce/magento-cart": "6.0.0-canary.37",
17
- "@graphcommerce/magento-cart-payment-method": "6.0.0-canary.37",
18
- "@graphcommerce/magento-cart-shipping-method": "6.0.0-canary.37",
19
- "@graphcommerce/magento-product": "6.0.0-canary.37",
20
- "@graphcommerce/next-config": "6.0.0-canary.37",
21
- "@graphcommerce/next-ui": "6.0.0-canary.37"
15
+ "@graphcommerce/graphql-mesh": "6.0.0-canary.39",
16
+ "@graphcommerce/magento-cart": "6.0.0-canary.39",
17
+ "@graphcommerce/magento-cart-payment-method": "6.0.0-canary.39",
18
+ "@graphcommerce/magento-cart-shipping-method": "6.0.0-canary.39",
19
+ "@graphcommerce/magento-product": "6.0.0-canary.39",
20
+ "@graphcommerce/next-config": "6.0.0-canary.39",
21
+ "@graphcommerce/next-ui": "6.0.0-canary.39"
22
22
  },
23
23
  "devDependencies": {
24
- "@graphcommerce/eslint-config-pwa": "6.0.0-canary.37",
25
- "@graphcommerce/prettier-config-pwa": "6.0.0-canary.37",
26
- "@graphcommerce/typescript-config-pwa": "6.0.0-canary.37",
24
+ "@graphcommerce/eslint-config-pwa": "6.0.0-canary.39",
25
+ "@graphcommerce/prettier-config-pwa": "6.0.0-canary.39",
26
+ "@graphcommerce/typescript-config-pwa": "6.0.0-canary.39",
27
27
  "@types/gtag.js": "^0.0.12"
28
28
  },
29
29
  "peerDependencies": {
@@ -6,30 +6,15 @@ export const component = 'FramerNextPages'
6
6
  export const exported = '@graphcommerce/framer-next-pages'
7
7
  export const ifConfig: IfConfig = 'googleAnalyticsId'
8
8
 
9
- let warned = false
10
-
11
9
  function GaFramerNextPages(props: PluginProps<PagesProps>) {
12
10
  const { Prev, ...rest } = props
13
11
 
14
- if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS) {
15
- return (
16
- <>
17
- <GoogleAnalyticsScript />
18
- <Prev {...rest} />
19
- </>
20
- )
21
- }
22
-
23
- if (process.env.NODE_ENV !== 'production') {
24
- if (!warned) {
25
- console.info(
26
- '[@graphcommerce/googleanalytics]: process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS not found',
27
- )
28
- warned = true
29
- }
30
- }
31
-
32
- return <Prev {...rest} />
12
+ return (
13
+ <>
14
+ <GoogleAnalyticsScript />
15
+ <Prev {...rest} />
16
+ </>
17
+ )
33
18
  }
34
19
 
35
20
  export const Plugin = GaFramerNextPages