Skip to content

Commit

Permalink
fix: fetch the lang and currency
Browse files Browse the repository at this point in the history
  • Loading branch information
marks-sre-is committed Oct 20, 2023
1 parent a32a8a2 commit 17b49bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
24 changes: 19 additions & 5 deletions starters/shopify-next-tailwind/app/compoents/ShopifyAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@ import {
getClientBrowserParameters,
AnalyticsEventName,
} from '@shopify/hydrogen-react';
import { CurrencyCode } from '@/lib/useMoney';
import { LanguageCode } from '@shopify/hydrogen-react/storefront-api-types';

export function sendPageView(shopId: string) {
export function sendPageView(
shopId: string,
currency: CurrencyCode,
acceptedLanguage: LanguageCode
) {
sendShopifyAnalytics({
eventName: AnalyticsEventName.PAGE_VIEW,
payload: {
...getClientBrowserParameters(),
hasUserConsent: true,
shopId: shopId,
currency: 'USD',
acceptedLanguage: 'EN',
currency: currency,
acceptedLanguage: acceptedLanguage,
},
});
}

function ShopifyAnalytics({ shopId }: { shopId: string }) {
function ShopifyAnalytics({
shopId,
currency,
acceptedLanguage,
}: {
shopId: string;
currency: CurrencyCode;
acceptedLanguage: LanguageCode;
}) {
const pathname = usePathname();

useEffect(() => {
sendPageView(shopId);
sendPageView(shopId, currency, acceptedLanguage);
}, [pathname]);

useShopifyCookies();
Expand Down
6 changes: 5 additions & 1 deletion starters/shopify-next-tailwind/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export default async function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<ShopifyAnalytics shopId={data.body.data.shop.id} />
<ShopifyAnalytics
shopId={data.body.data.shop.id}
currency={data.body.data.shop.currencyCode}
acceptedLanguage={data.body.data.shopLocales[0].locale}
/>
<div className="flex flex-col min-h-screen">
<div className="">
<a href="#mainContent" className="sr-only">
Expand Down
4 changes: 4 additions & 0 deletions starters/shopify-next-tailwind/lib/shopify/queries/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const LAYOUT_QUERY = `#graphql
id
name
description
currencyCode
primaryDomain {
url
}
Expand All @@ -19,6 +20,9 @@ export const LAYOUT_QUERY = `#graphql
}
}
}
shopLocales {
locale
}
headerMenu: menu(handle: $headerMenuHandle) {
id
items {
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

0 comments on commit 17b49bc

Please sign in to comment.