Skip to content

Provide extra option to GA config to prevent duplicate requests on page load #87

@jonathanrosenbaum

Description

@jonathanrosenbaum

Describe the bug

Full explanation here
#86

Problem
Bounce rates are too high in GA dashboard due to duplicate page view events on page load.

Fix
Provide the option to utilise the 'send_page_view' param to disable the first request.
https://developers.google.com/analytics/devguides/collection/gtagjs/pages

Code Changes Required
/services/google-analytics.js
Lines 23-46

exports.initializeGoogleAnalytics = function (options) {
  if (!window.gatsbyPluginGDPRCookiesGoogleAnalyticsInitialized && getCookie(options.cookieName) === "true" && validGATrackingId(options)) {
    window.dataLayer = window.dataLayer || [];

    window.gtag = function () {
      window.dataLayer.push(arguments);
    };

    window.gtag('js', new Date());
    var gaAnonymize = options.anonymize;
    var gaAllowAdFeatures = options.allowAdFeatures;
    var gaPageViewOnLoad = options.sendPageViewOnLoad

    gaAnonymize = gaAnonymize !== undefined ? gaAnonymize : true;
    gaAllowAdFeatures = gaAllowAdFeatures !== undefined ? gaAllowAdFeatures : true;
    gaPageViewOnLoad = gaPageViewOnLoad !== undefined ?  gaPageViewOnLoad : true;
    window.gtag('config', options.trackingId, {
      'anonymize_ip': gaAnonymize,
      'allow_google_signals': gaAllowAdFeatures,
      'send_page_view': gaPageViewOnLoad
    });
    window.gatsbyPluginGDPRCookiesGoogleAnalyticsInitialized = true;
  }
};

default-options.js
line 7-12

googleAnalytics: {
    cookieName: "gatsby-gdpr-google-analytics",
    anonymize: true,
    allowAdFeatures: false,
    sendPageViewOnLoad: false
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions