This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Initial GTM Migrations #456
Open
TuukkaIkius
wants to merge
7
commits into
main
Choose a base branch
from
TuukkaTahtinen/InitialGTM
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4599bec
feat: add isengaged hook
TuukkaIkius 0711d31
chore: add posthog engagedview tracking
TuukkaIkius 5353bab
chore: add qualified script
TuukkaIkius ccf405b
chore: add qualified page view
TuukkaIkius 8f273a4
chore: trigger initial load page views
TuukkaIkius dfb0aae
Merge branch 'main' into TuukkaTahtinen/InitialGTM
TuukkaIkius 1cc0cd4
Merge branch 'main' into TuukkaTahtinen/InitialGTM
TuukkaIkius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { useEffect } from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import Script from "next/script"; | ||
import type { AppProps } from "next/app"; | ||
import { DocsContextProvider } from "layouts/DocsPage/context"; | ||
import { posthog, sendPageview } from "utils/posthog"; | ||
import { posthog, sendEngagedView, sendPageview } from "utils/posthog"; | ||
import { TabContextProvider } from "components/Tabs"; | ||
|
||
// https://larsmagnus.co/blog/how-to-optimize-custom-fonts-with-next-font | ||
|
@@ -68,7 +68,32 @@ interface dataLayerItem { | |
declare global { | ||
var dataLayer: dataLayerItem[]; // eslint-disable-line no-var | ||
} | ||
const useIsEngaged = () => { | ||
const router = useRouter(); | ||
const [timerReached, setTimerReached] = useState(false); | ||
const [secondPageReached, setSecondPageReached] = useState(false); | ||
const [isEngaged, setIsEngaged] = useState(false); | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setTimerReached(true); | ||
}, 30000); | ||
const routeChanged = () => { | ||
setSecondPageReached(true); | ||
}; | ||
|
||
router.events.on("routeChangeComplete", routeChanged); | ||
return () => { | ||
router.events.off("routeChangeComplete", routeChanged); | ||
}; | ||
}, [router.events]); | ||
|
||
useEffect(() => { | ||
setIsEngaged(secondPageReached && timerReached); | ||
}, [secondPageReached, timerReached]); | ||
|
||
return isEngaged; | ||
}; | ||
const Analytics = () => { | ||
return ( | ||
<> | ||
|
@@ -163,6 +188,18 @@ const Analytics = () => { | |
{/* End Google Tag Manager (noscript) */} | ||
</> | ||
)} | ||
{/* Quailified Script */} | ||
<Script id="script_qualified"> | ||
{`(function (w, q) { | ||
w["QualifiedObject"] = q; | ||
w[q] = | ||
w[q] || | ||
function () { | ||
(w[q].q = w[q].q || []).push(arguments); | ||
}; | ||
})(window, "qualified")`} | ||
</Script> | ||
<Script src="https://js.qualified.com/qualified.js?token=GWPbwWJLtjykim4W" /> | ||
|
||
{NEXT_PUBLIC_REDDIT_ID && ( | ||
<> | ||
|
@@ -180,14 +217,29 @@ const Analytics = () => { | |
|
||
const MyApp = ({ Component, pageProps }: AppProps) => { | ||
const router = useRouter(); | ||
const isEngaged = useIsEngaged(); | ||
|
||
useEffect(() => { | ||
posthog(); // init posthog | ||
if (!isEngaged) return; | ||
// Trigger engagement view events here | ||
sendEngagedView(); | ||
}, [isEngaged]); | ||
Comment on lines
+223
to
+226
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to return an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this mainly comes down to which solution you perceive as being more readable. I think the current solution keeps the hook readable by isolating the tracking logic from the tracking events. |
||
|
||
router.events.on("routeChangeComplete", sendPageview); | ||
const Pageviews = () => { | ||
// Trigger page views here | ||
|
||
// Qualified page view | ||
if (!!window["qualified"]) window["qualified"]("page"); | ||
// Posthog page view | ||
sendPageview(); | ||
}; | ||
useEffect(() => { | ||
posthog(); // init posthog | ||
// Trigger initial load page views | ||
Pageviews(); | ||
router.events.on("routeChangeComplete", Pageviews); | ||
return () => { | ||
router.events.off("routeChangeComplete", sendPageview); | ||
router.events.off("routeChangeComplete", Pageviews); | ||
}; | ||
}, [router.events]); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this have to be two effects? From what I can tell from the logic, we are checking
Since the timer isn't being reset anywhere, we can just put
setIsEngaged
into the timeout instead right? this removes extra state and an extra useEffect. something like this pseudo code below. what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't quite match the criteria for tracking the engagement metrics.
With the current logic the timeout continues through page navigation and will trigger the tracking events when
With this proposed implementation the timeout resets on page navigation and the user would have to spend the full 30 seconds on the second (or any subsequent) page visited to be marked as an engaged viewer. This would cause a rapidly navigating user to not be marked as an engaged viewer.