UNPKG

gatsby-plugin-google-analytics-gdpr

Version:
139 lines (100 loc) 4.51 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.onRouteUpdate = exports.onClientEntry = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _reactGa = _interopRequireDefault(require("react-ga")); var _jsCookie = _interopRequireDefault(require("js-cookie")); require("clientjs"); var COOKIE_GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_COOKIES_ENABLED = "gatsby-plugin-google-analytics-gdpr_cookies-enabled"; // the following running modes are used with window.GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_RUNNING_WITH_MODE var GA_MODE_RUNNING_COOKIES_DISABLED = "runningCookiesDisabled"; var GA_MODE_RUNNING_COOKIES_ENABLED = "runningCookieEnabled"; function determineClientId() { var client = new ClientJS(); // eslint-disable-line no-undef var fingerprint = client.getFingerprint(); return fingerprint; } function initializeGA(pluginOptions) { var gaOptions = {}; if (!isCookiesEnabled()) { var clientFingerprint = determineClientId(); gaOptions = { storage: "none", // disable cookies for googe analytics clientId: clientFingerprint // set custom client fingerprint }; window.GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_RUNNING_WITH_MODE = GA_MODE_RUNNING_COOKIES_DISABLED; } else { window.GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_RUNNING_WITH_MODE = GA_MODE_RUNNING_COOKIES_ENABLED; } var ractGaOptions = { 'gaOptions': gaOptions }; if (pluginOptions.reactGaOptions !== undefined) { gaOptions = (0, _extends2.default)({}, pluginOptions.reactGaOptions.gaOptions, {}, gaOptions); ractGaOptions = (0, _extends2.default)({}, pluginOptions.reactGaOptions, {}, { 'gaOptions': gaOptions }); } _reactGa.default.initialize(pluginOptions.trackingId, ractGaOptions); _reactGa.default.set({ anonymizeIp: isAnonymizeIpEnabled(pluginOptions) }); } function isCookiesEnabled() { return _jsCookie.default.get(COOKIE_GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_COOKIES_ENABLED) === "true"; } function isAnonymizeIpEnabled(pluginOptions) { return pluginOptions.anonymizeIP !== undefined ? pluginOptions.anonymizeIP : true; } function isDevelopmentEnabled(pluginOptions) { return pluginOptions.enableDevelopment !== undefined ? pluginOptions.enableDevelopment : false; } var onClientEntry = function onClientEntry(_, pluginOptions) { if (pluginOptions === void 0) { pluginOptions = {}; } if (!pluginOptions.trackingId) { console.log("The Google Analytics GDPR plugin requires a tracking ID."); return null; } if (process.env.NODE_ENV !== 'production' && !isDevelopmentEnabled(pluginOptions)) { return null; } // start with setting from cookie if available // if no cookie settings is available, start with gatsby plugin settings 'autoStartWithCookiesEnabled' // if no plugin setting ist available, start with default setting (autoStartWithCookiesEnabled = false) if (_jsCookie.default.get(COOKIE_GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_COOKIES_ENABLED) === undefined) { var autoStartWithCookiesEnabled = pluginOptions.autoStartWithCookiesEnabled; autoStartWithCookiesEnabled = autoStartWithCookiesEnabled !== undefined ? autoStartWithCookiesEnabled : false; _jsCookie.default.set(COOKIE_GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_COOKIES_ENABLED, "" + autoStartWithCookiesEnabled); } initializeGA(pluginOptions); }; exports.onClientEntry = onClientEntry; function isGARunningInCorrectMode() { var runningWithCookiesEnabled = window.GATSBY_PLUGIN_GOOGLE_ANALYTICS_GDPR_RUNNING_WITH_MODE === GA_MODE_RUNNING_COOKIES_ENABLED; return isCookiesEnabled() === runningWithCookiesEnabled; } var onRouteUpdate = function onRouteUpdate(_ref, pluginOptions) { var location = _ref.location; if (pluginOptions === void 0) { pluginOptions = {}; } if (!pluginOptions.trackingId) { console.log("The Google Analytics GDPR plugin requires a tracking ID."); return null; } if (process.env.NODE_ENV !== 'production' && !isDevelopmentEnabled(pluginOptions)) { return null; } // restart google analytics with correct settings if needed if (!isGARunningInCorrectMode()) { _reactGa.default.ga("remove"); initializeGA(pluginOptions); } _reactGa.default.set({ page: location.pathname, anonymizeIp: isAnonymizeIpEnabled(pluginOptions) }); _reactGa.default.pageview(location.pathname); }; exports.onRouteUpdate = onRouteUpdate;