Skip to content

Commit

Permalink
Merge pull request #1418 from thematters/fix/firebase
Browse files Browse the repository at this point in the history
fix(tracking): overwrite default `page_referrer`
  • Loading branch information
robertu7 authored Aug 10, 2020
2 parents 8b82c0a + 7077d1a commit 2565e37
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/common/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -28,6 +28,10 @@ type EventArgs =
| ['view_add_credit_dialog', ViewDialogProp]
| ['view_donation_dialog', ViewDialogProp]

interface PageViewProp {
page_referrer: string
}

type ClickFeedProp =
| ArticleFeedProp
| CommentFeedProp
Expand Down
7 changes: 5 additions & 2 deletions src/components/Analytics/PageViewTracker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Router from 'next/router'
import { useContext, useEffect } from 'react'
import { useContext, useEffect, useRef } from 'react'

import { ViewerContext } from '~/components'

import { analytics } from '~/common/utils'

const PageViewTracker = () => {
const viewer = useContext(ViewerContext)
const referrer = useRef('')

// first load
useEffect(() => {
Expand All @@ -16,6 +17,7 @@ const PageViewTracker = () => {

analytics.identifyUser()
analytics.trackPage()
referrer.current = window.location.pathname
}, [viewer.privateFetched])

// subsequent changes
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/components/Analytics/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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])
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout/SideNav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
>
<NavListItem
Expand Down

0 comments on commit 2565e37

Please sign in to comment.