From 10e6702d541ebc3784118319a4bab2cd549d3dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Tkaczy=C5=84ski?= Date: Mon, 12 Feb 2018 22:57:43 +0100 Subject: [PATCH] feat(gatsby-plugin-google-analytics): add head option (#3995) Add head option to configure whether scrip tag should be put in the body (by default, same as previously) or inside of the head. closes #2894 --- packages/gatsby-plugin-google-analytics/README.md | 2 ++ .../gatsby-plugin-google-analytics/src/gatsby-ssr.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-google-analytics/README.md b/packages/gatsby-plugin-google-analytics/README.md index 1661e20fdebb7..c86042995fa05 100644 --- a/packages/gatsby-plugin-google-analytics/README.md +++ b/packages/gatsby-plugin-google-analytics/README.md @@ -15,6 +15,8 @@ plugins: [ resolve: `gatsby-plugin-google-analytics`, 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, }, diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js index 7e363d32f6dc7..6625543097a6a 100644 --- a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js @@ -1,8 +1,14 @@ import React from "react" -exports.onRenderBody = ({ setPostBodyComponents }, pluginOptions) => { +exports.onRenderBody = ( + { setHeadComponents, setPostBodyComponents }, + pluginOptions +) => { if (process.env.NODE_ENV === `production`) { - return setPostBodyComponents([ + const setComponents = pluginOptions.head + ? setHeadComponents + : setPostBodyComponents + return setComponents([