From b3ed9be6db7c80e11fe3fe046cf371b5e63fdbd6 Mon Sep 17 00:00:00 2001 From: tinder-kyleboss <42850683+tinder-kyleboss@users.noreply.github.com> Date: Tue, 30 Jul 2019 00:33:34 -0700 Subject: [PATCH] feat(gatsby-plugin-google-analytics): enable more options. (#15280) * Added more plugin options for gatsby-plugin-google-analytics. This included breaking out `knownOptions` into three factions: - createOnly - general All options are from general, but I did not add all of the general options because a few do not work in a set-it-and-forget-it kinda way. Added some tests. --- .../gatsby-plugin-google-analytics/README.md | 14 ++++-- .../src/__tests__/gatsby-ssr.js | 29 ++++++++++++ .../src/gatsby-ssr.js | 45 +++++++++++++------ 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/packages/gatsby-plugin-google-analytics/README.md b/packages/gatsby-plugin-google-analytics/README.md index 6b64c4c387bb7..963aef9919381 100644 --- a/packages/gatsby-plugin-google-analytics/README.md +++ b/packages/gatsby-plugin-google-analytics/README.md @@ -32,7 +32,7 @@ module.exports = { experimentId: "YOUR_GOOGLE_EXPERIMENT_ID", // Set Variation ID. 0 for original 1,2,3.... variationId: "YOUR_GOOGLE_OPTIMIZE_VARIATION_ID", - // Any additional create only fields (optional) + // Any additional optional fields sampleRate: 5, siteSpeedSampleRate: 10, cookieDomain: "example.com", @@ -42,7 +42,7 @@ module.exports = { } ``` -See below for the complete list of [Create Only Fields](#create-only-fields). +See below for the complete list of [optional fields](#optional-fields). ## `` component @@ -120,7 +120,7 @@ If you need to set up SERVER_SIDE Google Optimize experiment, you can add the ex Besides the experiment ID you also need the variation ID for SERVER_SIDE experiments in Google Optimize. Set 0 for original version. -## Create Only Fields +## Optional Fields This plugin supports all optional Create Only Fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create): @@ -138,4 +138,12 @@ This plugin supports all optional Create Only Fields documented in [Google Analy - `legacyHistoryImport`: boolean - `allowLinker`: boolean +This plugin also supports several optional General fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#general): + +- `allowAdFeatures`: boolean +- `dataSource`: string +- `queueTime`: number +- `forceSSL`: boolean +- `transport`: string + These fields can be specified in the plugin's `options` as shown in the [How to use](#how-to-use) section. diff --git a/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js index 9fe1eb00a9047..7ebc21e137ab2 100644 --- a/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js @@ -136,6 +136,35 @@ describe(`gatsby-plugin-google-analytics`, () => { expect(result).toMatch(/cookieName/) expect(result).toMatch(/sampleRate/) }) + + it(`sets additional general fields`, () => { + const { setPostBodyComponents } = setup({ + transport: `beacon`, + allowAdFeatures: true, + queueTime: 5, + }) + + const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) + expect(result).toContain(`ga('set', 'transport', 'beacon')`) + expect(result).toContain(`ga('set', 'allowAdFeatures', 'true')`) + expect(result).toContain(`ga('set', 'queueTime', '5')`) + }) + + it(`does not set fields that have an invalid value`, () => { + const { setPostBodyComponents } = setup({ + allowAdFeatures: `swag`, + }) + + const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) + expect(result).not.toContain(`allowAdFeatures`) + }) + + it(`does not set fields that were not set`, () => { + const { setPostBodyComponents } = setup({}) + + const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) + expect(result).not.toContain(`allowAdFeatures`) + }) }) }) }) diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js index e6ef02dd3bb3f..2a139018f4593 100644 --- a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js @@ -1,17 +1,26 @@ import React from "react" const knownOptions = { - clientId: `string`, - sampleRate: `number`, - siteSpeedSampleRate: `number`, - alwaysSendReferrer: `boolean`, - allowAnchor: `boolean`, - cookieName: `string`, - cookieExpires: `number`, - storeGac: `boolean`, - legacyCookieDomain: `string`, - legacyHistoryImport: `boolean`, - allowLinker: `boolean`, + createOnly: { + clientId: `string`, + sampleRate: `number`, + siteSpeedSampleRate: `number`, + alwaysSendReferrer: `boolean`, + allowAnchor: `boolean`, + cookieName: `string`, + cookieExpires: `number`, + storeGac: `boolean`, + legacyCookieDomain: `string`, + legacyHistoryImport: `boolean`, + allowLinker: `boolean`, + }, + general: { + allowAdFeatures: `boolean`, + dataSource: `string`, + queueTime: `number`, + forceSSL: `boolean`, + transport: `string`, + }, } export const onRenderBody = ( @@ -41,8 +50,8 @@ export const onRenderBody = ( } const gaCreateOptions = {} - for (const option in knownOptions) { - if (typeof pluginOptions[option] === knownOptions[option]) { + for (const option in knownOptions.createOnly) { + if (typeof pluginOptions[option] === knownOptions.createOnly[option]) { gaCreateOptions[option] = pluginOptions[option] } } @@ -109,7 +118,15 @@ export const onRenderBody = ( typeof pluginOptions.variationId !== `undefined` ? `ga('set', 'expVar', '${pluginOptions.variationId}');` : `` - }} + } + ${Object.keys(knownOptions.general).reduce((gaSetCommands, option) => { + if (typeof pluginOptions[option] === knownOptions.general[option]) { + gaSetCommands += `ga('set', '${option}', '${ + pluginOptions[option] + }');\n` + } + return gaSetCommands + }, ``)} `, }} />,