From 7077d1a270db595e3debd1f6265e15c3f210801c Mon Sep 17 00:00:00 2001 From: RoberTu <4065233+robertu7@users.noreply.github.com> Date: Mon, 10 Aug 2020 22:11:00 +0800 Subject: [PATCH] fix(tracking): overwrite default `page_referrer` --- src/common/utils/analytics.ts | 6 +++++- src/components/Analytics/PageViewTracker/index.tsx | 7 +++++-- src/components/Analytics/index.ts | 8 +++++++- src/components/Layout/SideNav/index.tsx | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/common/utils/analytics.ts b/src/common/utils/analytics.ts index 13bc3b24f7..2192a99a7f 100644 --- a/src/common/utils/analytics.ts +++ b/src/common/utils/analytics.ts @@ -18,7 +18,7 @@ export const analytics = { type EventArgs = | [] // identify user - | ['page_view'] // pageview + | ['page_view', PageViewProp] // pageview | ['pull_to_refresh'] | ['click_feed', ClickFeedProp] | ['click_button', ClickButtonProp] @@ -28,6 +28,10 @@ type EventArgs = | ['view_add_credit_dialog', ViewDialogProp] | ['view_donation_dialog', ViewDialogProp] +interface PageViewProp { + page_referrer: string +} + type ClickFeedProp = | ArticleFeedProp | CommentFeedProp diff --git a/src/components/Analytics/PageViewTracker/index.tsx b/src/components/Analytics/PageViewTracker/index.tsx index 05e48225bc..66ea073471 100644 --- a/src/components/Analytics/PageViewTracker/index.tsx +++ b/src/components/Analytics/PageViewTracker/index.tsx @@ -1,5 +1,5 @@ import Router from 'next/router' -import { useContext, useEffect } from 'react' +import { useContext, useEffect, useRef } from 'react' import { ViewerContext } from '~/components' @@ -7,6 +7,7 @@ import { analytics } from '~/common/utils' const PageViewTracker = () => { const viewer = useContext(ViewerContext) + const referrer = useRef('') // first load useEffect(() => { @@ -16,6 +17,7 @@ const PageViewTracker = () => { analytics.identifyUser() analytics.trackPage() + referrer.current = window.location.pathname }, [viewer.privateFetched]) // subsequent changes @@ -25,7 +27,8 @@ const PageViewTracker = () => { return } - analytics.trackPage() + analytics.trackPage('page_view', { page_referrer: referrer.current }) + referrer.current = window.location.pathname } Router.events.on('routeChangeComplete', trackPage) diff --git a/src/components/Analytics/index.ts b/src/components/Analytics/index.ts index 83008f4a7e..2380ff6d42 100644 --- a/src/components/Analytics/index.ts +++ b/src/components/Analytics/index.ts @@ -1,4 +1,5 @@ import gql from 'graphql-tag' +import _get from 'lodash/get' import { ANALYTIC_TYPES, ANALYTICS, GA_TRACKING_ID } from '~/common/enums' import { deferTry } from '~/common/utils' @@ -37,11 +38,16 @@ const handleAnalytics = ({ // GA & firebase tracking if (type === ANALYTIC_TYPES.PAGE) { const path = window.location.pathname + const referrer = _get(args[1], 'page_referrer') + window.gtag('config', GA_TRACKING_ID, { page_location: path, + page_referrer: referrer, }) - window.firebaseAnalytics.logEvent('page_view') + window.firebaseAnalytics.logEvent('page_view', { + page_referrer: referrer, + }) } else { window.firebaseAnalytics.logEvent(args[0], args[1]) } diff --git a/src/components/Layout/SideNav/index.tsx b/src/components/Layout/SideNav/index.tsx index 5024e87b66..4f8367a930 100644 --- a/src/components/Layout/SideNav/index.tsx +++ b/src/components/Layout/SideNav/index.tsx @@ -106,7 +106,7 @@ const SideNav = () => { placement="right-start" appendTo={process.browser ? document.body : undefined} offset={[-24, 24]} - zIndex={Z_INDEX.OVER_GLOBAL_HEADER} + zIndex={Z_INDEX.OVER_STICKY_TABS} onShown={hidePopperOnClick} >