Do not send analytics for dev mode

pull/1657/head
Kamran Ahmed 5 years ago
parent 0dd4734947
commit 3ab9ef0fa8
  1. 9
      components/helmet/index.js
  2. 10
      lib/gtag.js

@ -1,3 +1,4 @@
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
import NextHead from 'next/head'; import NextHead from 'next/head';
import siteConfig from 'storage/site'; import siteConfig from 'storage/site';
import { GA_TRACKING_ID } from 'lib/gtag'; import { GA_TRACKING_ID } from 'lib/gtag';
@ -22,13 +23,13 @@ const Helmet = (props) => (
<meta name="keywords" content={ siteConfig.keywords.join(',') } /> <meta name="keywords" content={ siteConfig.keywords.join(',') } />
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=3.0, minimum-scale=1.0" /> <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=3.0, minimum-scale=1.0" />
<link rel="canonical" href={ siteConfig.url.web } /> <link rel="canonical" href={ siteConfig.url } />
<meta httpEquiv="Content-Language" content="en" /> <meta httpEquiv="Content-Language" content="en" />
<meta property="og:title" content={ prepareTitle(props.title) } /> <meta property="og:title" content={ prepareTitle(props.title) } />
<meta property="og:description" content={ prepareDescription(props.description) } /> <meta property="og:description" content={ prepareDescription(props.description) } />
<meta property="og:image" content={ siteConfig.logo } /> <meta property="og:image" content={ siteConfig.logo } />
<meta property="og:url" content={ siteConfig.url.web } /> <meta property="og:url" content={ siteConfig.url } />
<meta property="og:type" content="website" /> <meta property="og:type" content="website" />
<meta property="article:publisher" content={ `https://facebook.com/${siteConfig.facebook}` } /> <meta property="article:publisher" content={ `https://facebook.com/${siteConfig.facebook}` } />
<meta property="og:site_name" content={ siteConfig.name } /> <meta property="og:site_name" content={ siteConfig.name } />
@ -55,6 +56,8 @@ const Helmet = (props) => (
<link rel="icon" href="/static/manifest/favicon.ico" type="image/x-icon" /> <link rel="icon" href="/static/manifest/favicon.ico" type="image/x-icon" />
{ /* Global Site Tag (gtag.js) - Google Analytics */ } { /* Global Site Tag (gtag.js) - Google Analytics */ }
{ !PHASE_DEVELOPMENT_SERVER && (
<>
<script async src={ `https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}` } /> <script async src={ `https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}` } />
<script dangerouslySetInnerHTML={{ <script dangerouslySetInnerHTML={{
__html: ` __html: `
@ -64,6 +67,8 @@ const Helmet = (props) => (
gtag('config', '${GA_TRACKING_ID}'); gtag('config', '${GA_TRACKING_ID}');
`, `,
}} /> }} />
</>
)}
</NextHead> </NextHead>
); );

@ -2,6 +2,11 @@ export const GA_TRACKING_ID = 'UA-139582634-1';
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages // https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const firePageView = url => { export const firePageView = url => {
if (!window.gtag) {
console.warn('Missing GTAG – Analytics disabled');
return;
}
window.gtag('config', GA_TRACKING_ID, { window.gtag('config', GA_TRACKING_ID, {
page_path: url, page_path: url,
}) })
@ -9,6 +14,11 @@ export const firePageView = url => {
// https://developers.google.com/analytics/devguides/collection/gtagjs/events // https://developers.google.com/analytics/devguides/collection/gtagjs/events
export const event = ({ action, category, label, value }) => { export const event = ({ action, category, label, value }) => {
if (!window.gtag) {
console.warn('Missing GTAG – Analytics disabled');
return;
}
window.gtag('event', action, { window.gtag('event', action, {
event_category: category, event_category: category,
event_label: label, event_label: label,

Loading…
Cancel
Save