1

我想在我的网站中使用谷歌分析,但要符合 gdpr,所以只有在用户同意时才触发它。

我正在使用 gatsby 并遵循本教程:https ://www.improvebadcode.com/gatsby-gdpr-cookie-consent/ ,这在我的理解中完全有意义。

所以我正在使用gatsby-plugin-gdpr-cookiesand react-cookie-consent

我的 gatsby 配置如下所示:

plugins: [
    {
      resolve: `gatsby-plugin-gdpr-cookies`,
      options: {
        googleAnalytics: {
          trackingId: '---', // leave empty if you want to disable the tracker
          cookieName: 'gatsby-gdpr-google-analytics', // default
          anonymize: true, // default
        },
        // defines the environments where the tracking should be available  - default is ["production"]
        environments: ['production', 'development'],
      },
    },

和我的 App.js 文件中的 cookie 同意,如下所示:

<CookieConsent
        enableDeclineButton
        flipButtons
        location="bottom"
        buttonText="Zustimmen"
        declineButtonStyle={{ background: '#5f7063', border: 'solid grey 1px', color: 'grey' }}
        style={{ background: '#5f7063' }}
        declineButtonText="Ablehnen"
        buttonStyle={{
          backgroundColor: '#fff',
          color: '#000',
          fontSize: '13px',
        }}
        cookieName="gatsby-gdpr-google-analytics"
      >
        Diese Website speichert Cookies auf Ihrem Computer. ...
      </CookieConsent>

在 gatsby 构建我的 cookie 横幅后,它显示得非常好,但我没有收到任何关于我的谷歌分析的数据。

我首先认为问题是我使用的是 GA4 版本的 GA,但我生成了一个“旧”的 Universal Analytics 标记,但它仍然无法正常工作。

谁能告诉我,我做错了什么?

当我在我的网站上查找谷歌分析时,这是我网站上的输出:

 var options = (0, _merge.default)(_defaultOptions.default, pluginOptions);

  if (isEnvironmentValid(options.environments)) {
    // google analytics
    initGoogleAnalytics(options); // facebook pixel

    initFacebookPixel(options);
  }
}; // initializing helpers


exports.onClientEntry = onClientEntry;

var initGoogleAnalytics = function initGoogleAnalytics(options) {
  if (cookies.get(options.googleAnalytics.cookieName) === "true" && (0, _validTrackingId.validGATrackingId)(options)) {
    _reactGa.default.initialize(options.googleAnalytics.trackingId);

    window.GoogleAnalyticsIntialized = true;
  }
};

var initFacebookPixel = function initFacebookPixel(options) {
  if (cookies.get(options.facebookPixel.cookieName) === "true" && (0, _validTrackingId.validFbPixelId)(options) && typeof window.fbq === "function") {
    window.fbq("init", options.facebookPixel.pixelId);
    window.FacebookPixelInitialized = true;
  }
};

var checkIfGoogleAnalyticsIsInitilized = function checkIfGoogleAnalyticsIsInitilized() {
  return !!window.GoogleAnalyticsIntialized;
};

var checkIfFacebookPixelIsInitilized = function checkIfFacebookPixelIsInitilized() {
  return !!window.FacebookPixelInitialized;
}; // track


var onRouteUpdate = function onRouteUpdate(_ref, pluginOptions) {
  var location = _ref.location;

  if (pluginOptions === void 0) {
    pluginOptions = {};
  }

  var options = (0, _merge.default)(_defaultOptions.default, pluginOptions);

  if (isEnvironmentValid(options.environments)) {
    // google analytics
    if (!checkIfGoogleAnalyticsIsInitilized()) initGoogleAnalytics(options);

    if (cookies.get(options.googleAnalytics.cookieName) === "true" && (0, _validTrackingId.validGATrackingId)(options) && _reactGa.default.ga) {
      var gaAnonymize = options.googleAnalytics.anonymize;
      var gaAllowAdFeatures = options.googleAnalytics.allowAdFeatures;
      gaAnonymize = gaAnonymize !== undefined ? gaAnonymize : true;
      gaAllowAdFeatures = gaAllowAdFeatures !== undefined ? gaAllowAdFeatures : true;

      _reactGa.default.set({
        page: location.pathname,
        anonymizeIp: gaAnonymize,
        allowAdFeatures: gaAllowAdFeatures
      });

      _reactGa.default.pageview(location.pathname);
    } // google tag manager


    if (cookies.get(options.googleTagManager.cookieName) === "true" && (0, _validTrackingId.validGTMTrackingId)(options)) {
      setTimeout(function () {
        var data = options.googleTagManager.dataLayerName ? window[options.googleTagManager.dataLayerName] : window.dataLayer;

        if (typeof data === "object") {
          var eventName = options.googleTagManager.routeChangeEvent || "gatsbyRouteChange";
          data.push({
            event: eventName
          });
        }
      }, 50);
    } // facebook pixel


    if (!checkIfFacebookPixelIsInitilized()) initFacebookPixel(options);

    if (cookies.get(options.facebookPixel.cookieName) === "true" && (0, _validTrackingId.validFbPixelId)(options) && typeof window.fbq === "function") {
      window.fbq("track", "PageView");
    }
  }
};

exports.onRouteUpdate = onRouteUpdate;
4

1 回答 1

0

我最近遇到了同样的问题,一些谷歌分析插件(gatsby-plugin-gdpr-cookiesgatsby-plugin-google-analytics)。似乎两者实际上都在使用旧版本的跟踪器。该脚本完美地插入到页面中,但在 Google 的仪表板中没有显示任何结果。

阅读一些我已经结束使用的官方文档gatsby-plugin-google-gtag(由 Gatsby 推荐)并且它有效,也许它也适用于你:

// In your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        // You can add multiple tracking ids and a pageview event will be fired for all of them.
        trackingIds: [
          "GA-TRACKING_ID", // Google Analytics / GA
          "AW-CONVERSION_ID", // Google Ads / Adwords / AW 
          "DC-FLOODIGHT_ID", // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
        ],
        // This object gets passed directly to the gtag config command
        // This config will be shared across all trackingIds
        gtagConfig: {
          optimize_id: "OPT_CONTAINER_ID",
          anonymize_ip: true,
          cookie_expires: 0,
        },
        // This object is used for configuration specific to this plugin
        pluginConfig: {
          // Puts tracking script in the head instead of the body
          head: false,
          // Setting this parameter is also optional
          respectDNT: true,
          // Avoids sending pageview hits from custom paths
          exclude: ["/preview/**", "/do-not-track/me/too/"],
        },
      },
    },
  ],
}

您可以省略/删除所有可选参数并替换为GA-TRACKING_ID您的。

于 2020-11-21T09:21:31.373 回答