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

Cleanup GTM code. #1428

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions dapp/lib/gtm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export const pageview = () => {
pageUrl: window.location.href,
pageTitle: document.title,
})
}

export const event = (eventData) => {
window?.dataLayer?.push(eventData)
}
104 changes: 24 additions & 80 deletions dapp/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ import UserActivityListener from 'components/UserActivityListener'
import TransactionListener from 'components/TransactionListener'
import withWeb3Provider from 'hoc/withWeb3Provider'
import setUtilLocale from 'utils/setLocale'
import { setUserSource } from 'utils/user'
import { useEagerConnect } from 'utils/hooks'
import { login } from 'utils/account'
import WalletSelectModal from 'components/WalletSelectModal'
import { ToastContainer } from 'react-toastify'
import { pageview } from '../lib/gtm'

import analytics from 'utils/analytics'
import { AnalyticsProvider } from 'use-analytics'
import { initSentry } from 'utils/sentry'

import 'react-toastify/scss/main.scss'
Expand All @@ -46,11 +43,11 @@ function App({ Component, pageProps, err }) {
).split('?')[0]

useEffect(() => {
router.events.on('routeChangeComplete', pageview)
router.events.on("routeChangeComplete", pageview);
return () => {
router.events.off('routeChangeComplete', pageview)
}
}, [router.events])
router.events.off("routeChangeComplete", pageview);
};
}, [router.events]);

useEffect(() => {
// Update account info when connection already established
Expand All @@ -66,57 +63,6 @@ function App({ Component, pageProps, err }) {
}
}, [])

const trackPageView = (url, lastURL) => {
const data = {
toURL: url,
}

if (lastURL) {
data.fromURL = lastURL
}

analytics.page(data)

if (url.indexOf('?') > 0) {
const searchParams = new URLSearchParams(url.substr(url.indexOf('?') + 1))
const utmSource = searchParams.get('utm_source')
if (utmSource) {
setUserSource(utmSource)
}
} else {
/* if first page load is not equipped with the 'utm_source' we permanently mark
* user source as unknown
*/
setUserSource('unknown')
}
}

useEffect(() => {
let lastURL = window.location.pathname + window.location.search

// track initial page load
trackPageView(lastURL)

const handleRouteChange = (url) => {
/* There is this weird behaviour with react router where `routeChangeComplete` gets triggered
* on initial load only if URL contains search parameters. And without this check and search
* parameters present the inital page view would be tracked twice.
*/
if (url === lastURL) {
return
}
// track when user navigates to a new page
trackPageView(url, lastURL)
lastURL = url
}

router.events.on('routeChangeComplete', handleRouteChange)

return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])

const onLocale = async (newLocale) => {
const locale = await setUtilLocale(newLocale)
setLocale(locale)
Expand All @@ -129,28 +75,26 @@ function App({ Component, pageProps, err }) {
<link rel="canonical" href={canonicalUrl} />
</Head>
<QueryClientProvider client={queryClient}>
<AnalyticsProvider instance={analytics}>
<AccountListener />
<TransactionListener />
<UserActivityListener />
<WalletSelectModal />
<ToastContainer
position="bottom-right"
autoClose={5000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
pauseOnHover
/>
<Component
locale={locale}
onLocale={onLocale}
{...pageProps}
err={err}
/>
</AnalyticsProvider>
<AccountListener />
<TransactionListener />
<UserActivityListener />
<WalletSelectModal />
<ToastContainer
position="bottom-right"
autoClose={5000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
pauseOnHover
/>
<Component
locale={locale}
onLocale={onLocale}
{...pageProps}
err={err}
/>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</>
Expand Down