Files
30-seconds-of-code/node_modules/gatsby-plugin-google-analytics/gatsby-ssr.js
2019-08-20 15:52:05 +02:00

81 lines
4.1 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.onRenderBody = void 0;
var _react = _interopRequireDefault(require("react"));
var knownOptions = {
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"
}
};
var onRenderBody = function onRenderBody(_ref, pluginOptions) {
var setHeadComponents = _ref.setHeadComponents,
setPostBodyComponents = _ref.setPostBodyComponents;
if (process.env.NODE_ENV !== "production") {
return null;
} // Lighthouse recommends pre-connecting to google analytics
setHeadComponents([_react.default.createElement("link", {
rel: "preconnect dns-prefetch",
key: "preconnect-google-analytics",
href: "https://www.google-analytics.com"
})]);
var excludeGAPaths = [];
if (typeof pluginOptions.exclude !== "undefined") {
var Minimatch = require("minimatch").Minimatch;
pluginOptions.exclude.map(function (exclude) {
var mm = new Minimatch(exclude);
excludeGAPaths.push(mm.makeRe());
});
}
var gaCreateOptions = {};
for (var option in knownOptions.createOnly) {
if (typeof pluginOptions[option] === knownOptions.createOnly[option]) {
gaCreateOptions[option] = pluginOptions[option];
}
}
var setComponents = pluginOptions.head ? setHeadComponents : setPostBodyComponents;
return setComponents([_react.default.createElement("script", {
key: "gatsby-plugin-google-analytics",
dangerouslySetInnerHTML: {
__html: "\n " + (excludeGAPaths.length ? "window.excludeGAPaths=[" + excludeGAPaths.join(",") + "];" : "") + "\n " + (typeof pluginOptions.anonymize !== "undefined" && pluginOptions.anonymize === true ? "function gaOptout(){document.cookie=disableStr+'=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/',window[disableStr]=!0}var gaProperty='" + pluginOptions.trackingId + "',disableStr='ga-disable-'+gaProperty;document.cookie.indexOf(disableStr+'=true')>-1&&(window[disableStr]=!0);" : "") + "\n if(" + (typeof pluginOptions.respectDNT !== "undefined" && pluginOptions.respectDNT == true ? "!(navigator.doNotTrack == \"1\" || window.doNotTrack == \"1\")" : "true") + ") {\n (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');\n }\n if (typeof ga === \"function\") {\n ga('create', '" + pluginOptions.trackingId + "', '" + (typeof pluginOptions.cookieDomain === "string" ? pluginOptions.cookieDomain : "auto") + "', " + (typeof pluginOptions.name === "string" ? "'" + pluginOptions.name + "', " : "") + JSON.stringify(gaCreateOptions) + ");\n " + (typeof pluginOptions.anonymize !== "undefined" && pluginOptions.anonymize === true ? "ga('set', 'anonymizeIp', true);" : "") + "\n " + (typeof pluginOptions.optimizeId !== "undefined" ? "ga('require', '" + pluginOptions.optimizeId + "');" : "") + "\n " + (typeof pluginOptions.experimentId !== "undefined" ? "ga('set', 'expId', '" + pluginOptions.experimentId + "');" : "") + "\n " + (typeof pluginOptions.variationId !== "undefined" ? "ga('set', 'expVar', '" + pluginOptions.variationId + "');" : "") + "\n " + Object.keys(knownOptions.general).reduce(function (gaSetCommands, option) {
if (typeof pluginOptions[option] === knownOptions.general[option]) {
gaSetCommands += "ga('set', '" + option + "', '" + pluginOptions[option] + "');\n";
}
return gaSetCommands;
}, "") + "\n }"
}
})]);
};
exports.onRenderBody = onRenderBody;