Skip to content

Commit

Permalink
Update _layout.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
xHomu committed Sep 3, 2024
1 parent 011a7ff commit f598310
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions app/routes/_site+/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,17 @@ export async function loader({

export default function SiteLayout() {
const { site } = useLoaderData<typeof loader>() || {};
const adWebId = site?.adWebId;
const location = useLocation();
const isBot = useIsBot();

const [isPrimaryMenu, setPrimaryMenuOpen] = useState(false);

const adWebId = site?.adWebId;
const gaTrackingId = site?.gaTagId;

useEffect(() => {
if (gaTrackingId) {
gtag.pageview(location.pathname, gaTrackingId);
}
}, [location, gaTrackingId]);

return (
<>
{process.env.NODE_ENV === "production" && gaTrackingId && !isBot && (
<>
<script
defer
src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`}
/>
<script
defer
id="gtag-init"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaTrackingId}', {
page_path: window.location.pathname,
});
`,
}}
/>
</>
{process.env.NODE_ENV === "production" && !isBot && gaTrackingId && (
<GAScript gaTrackingId={gaTrackingId} />
)}
<MobileHeader />
<main
Expand All @@ -91,7 +64,7 @@ export default function SiteLayout() {
</section>
<ColumnFour />
</main>
{process.env.NODE_ENV === "production" && gaTrackingId && !isBot && (
{process.env.NODE_ENV === "production" && !isBot && adWebId && (
<RampInit adWebId={adWebId} />
)}
<AdUnit
Expand All @@ -107,6 +80,37 @@ export default function SiteLayout() {
);
}

export function GAScript({ gaTrackingId }: { gaTrackingId: string }) {
const location = useLocation();
useEffect(() => {
gtag.pageview(location.pathname, gaTrackingId);
}, [location, gaTrackingId]);

return (
<>
<script
defer
src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`}
/>
<script
defer
id="gtag-init"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaTrackingId}', {
page_path: window.location.pathname,
});
`,
}}
/>
</>
);
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return [
{
Expand Down

0 comments on commit f598310

Please sign in to comment.