Skip to content

Commit

Permalink
Include Google Analytics code
Browse files Browse the repository at this point in the history
This PR adds the Google Analytics code snippet. It also creates a new environment variable, ENABLE_GOOGLE_ANALYTICS, that must be set to 'enabled' for the GA snippet to render.
  • Loading branch information
kalvinwang authored Sep 3, 2021
2 parents b7de898 + dddae80 commit 2059d99
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface HomeProps {
errorCode?: number | null
userArrivedFromUioMobile?: boolean
urlPrefixes: UrlPrefixes
enableGoogleAnalytics: string
}

export default function Home({
Expand All @@ -34,9 +35,34 @@ export default function Home({
errorCode = null,
userArrivedFromUioMobile = false,
urlPrefixes,
enableGoogleAnalytics,
}: HomeProps): ReactElement {
const { t } = useTranslation('common')

// Once CSP is enabled, if you change the GA script code, you need to update the hash in csp.js to allow
// this script to run. Chrome dev tools will have an error with the correct hash value to use.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script
const gaScript = `
window.dataLayer = window.dataLayer || []
function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
// For details see: https://support.google.com/analytics/answer/9310895?hl=en
// https://developers.google.com/analytics/devguides/collection/gtagjs/ip-anonymization
gtag('config', 'UA-3419582-2', { 'anonymize_ip': true }) // www.ca.gov
gtag('config', 'UA-3419582-31', { 'anonymize_ip': true }) // edd.ca.gov`

const googleAnalytics = (
<>
{/* Global site tag (gtag.js) - Google Analytics */}
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-3419582-2" />
<script
dangerouslySetInnerHTML={{
__html: gaScript,
}}
/>
</>
)

// If any errorCode is provided, render the error page.
let mainComponent: JSX.Element
if (errorCode) {
Expand Down Expand Up @@ -75,6 +101,7 @@ export default function Home({
href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet"
/>
{enableGoogleAnalytics === 'enabled' && googleAnalytics}
</Head>
<Header userArrivedFromUioMobile={userArrivedFromUioMobile} />
{mainComponent}
Expand All @@ -98,6 +125,9 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res, locale,
urlPrefixBpo: process.env.URL_PREFIX_BPO ?? '',
}

// Set to 'enabled' to include Google Analytics code
const ENABLE_GOOGLE_ANALYTICS = process.env.ENABLE_GOOGLE_ANALYTICS ?? ''

// Other vars.
let errorCode: number | null = null
let scenarioContent: ScenarioContent | null = null
Expand Down Expand Up @@ -168,6 +198,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res, locale,
errorCode: errorCode,
userArrivedFromUioMobile: userArrivedFromUioMobile,
urlPrefixes: URL_PREFIXES,
enableGoogleAnalytics: ENABLE_GOOGLE_ANALYTICS,
...(await serverSideTranslations(locale || 'en', ['common', 'claim-details', 'claim-status'])),
},
}
Expand Down

0 comments on commit 2059d99

Please sign in to comment.