Gatsby: Add support for Google Optimize within the Google Analytics plugin

Created on 13 Sep 2018  ·  2Comments  ·  Source: gatsbyjs/gatsby

Summary

Within the Google Analytics Plugin, there isn't currently a way to specific a Google Optimize container id to allow for easy A/B testing.

Basic example

Here's a link to the Google Optimize recommended setup: https://support.google.com/optimize/answer/6262084?hl=en

This method basically boils down to adding ga('require', 'GTM-XXXXXX'); where GTM-XXXXXX is the container id. Here's an example of where this line is added:

    ga('create', 'UA-XXXXXXXX-X', 'auto');
    ga('require', 'GTM-XXXXXX');
    ga('send', 'pageview');

It seems that this could easily be added to the existing google analytics plugin, following the anonymize line:

+ (typeof pluginOptions.anonymize !== "undefined" ? "ga('set', 'anonymizeIp', 1);" : "") + "\n "     
+ (typeof pluginOptions.optimizeId !== "undefined" ? "ga('require', " + pluginOptions.optimizeId + ");" : "") + "}\n "

And would allow for setting this Id in gatsby-config:

options: {
        trackingId: "YOUR_GOOGLE_ANALYTICS_TRACKING_ID",
        // Puts tracking script in the head instead of the body
        head: false,
        // Setting this parameter is optional
        anonymize: true,
        // Setting this parameter is also optional
        respectDNT: true,
        // Avoids sending pageview hits from custom paths
        exclude: ["/preview/**", "/do-not-track/me/too/"],
        optimizeId: "YOUR_GOOGLE_OPTIMIZE_TRACKING_ID",
        // Enables Google Optimize using your Id
      },

Motivation

Adding support for a Google Optimize Id builds on the already existing support for Google Analytics + Google Tag Manager and makes it very simple for users to easily perform complex A/B + multivariate tests on their existing Gatsby sites.

Would be happy to submit a PR with this addition and welcome any feedback beforehand!

good first issue help wanted

Most helpful comment

Thanks @kakadiadarpan!

Closing this issue as the PR that adds this functionality has been successfully merged 🎉

All 2 comments

We'd love to have your help, @jonhorton! Please feel free to submit a PR since you already know what changes to make. This is where you will need to make the changes.

Thanks @kakadiadarpan!

Closing this issue as the PR that adds this functionality has been successfully merged 🎉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimfilippou picture jimfilippou  ·  3Comments

signalwerk picture signalwerk  ·  3Comments

ghost picture ghost  ·  3Comments

ferMartz picture ferMartz  ·  3Comments

Oppenheimer1 picture Oppenheimer1  ·  3Comments