Skip to content

Commit

Permalink
Merge pull request #295 from nulib/4165-collection-gtm
Browse files Browse the repository at this point in the history
Debug GTM VirtualPageView events, add `collections` data to `/collections/*` routes.
  • Loading branch information
mathewjordan authored Sep 13, 2023
2 parents a2a5eca + be29625 commit d7f3ee9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
camptonExtraBold,
camptonExtraLight,
} from "@/styles/fonts";

import type { AppProps } from "next/app";
import Head from "next/head";
import { ObjectLiteral } from "@/types";
Expand Down Expand Up @@ -44,19 +45,27 @@ function MyApp({ Component, pageProps }: MyAppProps) {
getData();
}, []);

{
/** Add GTM (Google Tag Manager) data */
}
React.useEffect(() => {
if (typeof window !== "undefined" && mounted) {
const payload = {
event: "VirtualPageView",
// @ts-ignore
...pageProps.dataLayer,
isLoggedIn: user?.isLoggedIn,
};
const { dataLayer } = pageProps;

// @ts-ignore
window.dataLayer?.push(payload);
if (dataLayer?.pageTitle) {
const payload = {
...pageProps.dataLayer,
isLoggedIn: user?.isLoggedIn,
};

// send pageProps to dataLayer
// @ts-ignore
window.dataLayer?.push(payload);

// send VirtualPageView event to dataLayer
// @ts-ignore
window.dataLayer?.push({
event: "VirtualPageView",
});
}
}
}, [mounted, pageProps, user]);

Expand Down
2 changes: 1 addition & 1 deletion pages/collections/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
/** Add values to GTM's dataLayer object */
const dataLayer = buildDataLayer({
adminset: "",
collections: "",
collections: collection?.title,
creatorsContributors: "",
isLoggedIn: false,
pageTitle: (collection?.title as string) || "",
Expand Down

0 comments on commit d7f3ee9

Please sign in to comment.