Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Shopify Analytics #1333

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading