diff --git a/starters/shopify-next-tailwind/README.md b/starters/shopify-next-tailwind/README.md index 327c57c55..bcc51ebda 100644 --- a/starters/shopify-next-tailwind/README.md +++ b/starters/shopify-next-tailwind/README.md @@ -98,6 +98,13 @@ git clone https://github.com/thisdot/starter.dev.git - `pnpm run format` - Formats code for the entire project. - `pnpm run format.check` - Checks all project code to conform to prettier rules. +## Shopify Analytics + +By default, Shopify Analytics is enabled. There are 2 main options you have to set them in the code from `app/components/ShopifyAnalytics.tsx`: + +- `currency` - The currency used in the store. nd by default is set to `USD`. +- `acceptedLanguages` - The languages accepted by the store. By default is set to `US`. + ## Demo Implementation [Live Store](https://shopify-next-tailwind.starter.dev) diff --git a/starters/shopify-next-tailwind/app/compoents/ShopifyAnalytics.tsx b/starters/shopify-next-tailwind/app/compoents/ShopifyAnalytics.tsx new file mode 100644 index 000000000..3d0e9c3db --- /dev/null +++ b/starters/shopify-next-tailwind/app/compoents/ShopifyAnalytics.tsx @@ -0,0 +1,47 @@ +'use client'; + +import { usePathname } from 'next/navigation'; +import { useEffect } from 'react'; +import { + sendShopifyAnalytics, + getClientBrowserParameters, + AnalyticsEventName, + useShopifyCookies, +} from '@shopify/hydrogen-react'; +import { CurrencyCode } from '@/lib/useMoney'; +import { LanguageCode } from '@shopify/hydrogen-react/storefront-api-types'; + +const ANALYTICS_CONFIG: { + currency: CurrencyCode; + acceptedLanguage: LanguageCode; +} = { + currency: 'USD', + acceptedLanguage: 'EN', +}; + +export function sendPageView(shopId: string) { + sendShopifyAnalytics({ + eventName: AnalyticsEventName.PAGE_VIEW, + payload: { + ...getClientBrowserParameters(), + hasUserConsent: true, + shopId: shopId, + currency: ANALYTICS_CONFIG.currency, + acceptedLanguage: ANALYTICS_CONFIG.acceptedLanguage, + }, + }); +} + +function ShopifyAnalytics({ shopId }: { shopId: string }) { + const pathname = usePathname(); + + useEffect(() => { + sendPageView(shopId); + }, [pathname]); + + useShopifyCookies(); + + return <>>; +} + +export default ShopifyAnalytics; diff --git a/starters/shopify-next-tailwind/app/layout.tsx b/starters/shopify-next-tailwind/app/layout.tsx index 51cdca0b4..b8aaf7210 100644 --- a/starters/shopify-next-tailwind/app/layout.tsx +++ b/starters/shopify-next-tailwind/app/layout.tsx @@ -7,6 +7,7 @@ import Footer from './compoents/Footer'; import Header from './compoents/Header'; import { cookies } from 'next/headers'; import { Metadata } from 'next'; +import ShopifyAnalytics from './compoents/ShopifyAnalytics'; const inter = Inter({ subsets: ['latin'] }); @@ -27,6 +28,7 @@ export default async function RootLayout({ return (
+