Skip to content

Commit

Permalink
feat: Shopify Analytics (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
markshenouda authored Oct 20, 2023
1 parent 2c94509 commit e233147
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions starters/shopify-next-tailwind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
47 changes: 47 additions & 0 deletions starters/shopify-next-tailwind/app/compoents/ShopifyAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions starters/shopify-next-tailwind/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });

Expand All @@ -27,6 +28,7 @@ export default async function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<ShopifyAnalytics shopId={data.body.data.shop.id} />
<div className="flex flex-col min-h-screen">
<div className="">
<a href="#mainContent" className="sr-only">
Expand Down
5 changes: 5 additions & 0 deletions starters/shopify-next-tailwind/lib/shopify/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { FiltersQueryParams } from '@/app/collections/[collectionHandle]/page';
import { CollectionHero } from '@/components/Hero';
import { LanguageCode } from '@shopify/hydrogen-react/storefront-api-types';

export type Shop = {
id: string;
name: string;
description: string | null;
currencyCode: CurrencyCode;
primaryDomain: {
url: string;
};
Expand Down Expand Up @@ -77,6 +79,9 @@ export type ShopifyFooterItem = {
export type ShopifyLayoutOperation = {
data: {
shop: Shop;
shopLocales: {
locale: LanguageCode;
}[];
headerMenu: ShopifyHeaderMenu;
footerMenu: ShopifyFooterMenu;
};
Expand Down
1 change: 1 addition & 0 deletions starters/shopify-next-tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@babel/core": "7.22.9",
"@headlessui/react": "1.7.15",
"@shopify/hydrogen-react": "^2023.7.4",
"@storybook/addon-styling": "1.3.4",
"@storybook/testing-library": "0.2.0",
"@tailwindcss/forms": "0.5.4",
Expand Down

0 comments on commit e233147

Please sign in to comment.