简体   繁体   English

类型错误:非法调用

[英]TypeError: Illegal Invocation

We're running a Create React App (CRA) web app, to which we've added Google Analytics v4.我们正在运行一个 Create React App (CRA) web 应用程序,我们在其中添加了 Google Analytics v4。 We initiate analytics using the ga-4-react npm package.我们使用ga-4-react npm package 启动分析。 Manual initialization in index.js index.js中的手动初始化

const ga4react = new GA4React(process.env.REACT_APP_GOOGLE_ANALYTICS_ID);

ga4react.initialize().then((ga4) => {
    ga4.pageview(window.location.path);
}, (err) => {
    Sentry.captureException(err);
});

We're receiving hundreds of errors from the gtag/js file to our Sentry error monitoring platform.我们从gtag/js文件中收到数百个错误到我们的 Sentry 错误监控平台。 This error doesn't really say much to us and we've spent two days trying to find out if anyone's run into a problem like this, but so far we've come up empty.这个错误对我们来说并没有什么太大的意义,我们花了两天时间试图找出是否有人遇到过这样的问题,但到目前为止我们还是一无所获。 It also doesn't seem to affect user experience, but it's rather annoying for us to monitor.它似乎也不会影响用户体验,但是对于我们来说监控它是相当烦人的。

The error is reported as so to Sentry.错误会这样报告给 Sentry。

TypeError zq(gtag/js)
Illegal invocation

gtag/js in zq at line 477:453
{snip} ))}}},zq=function(a,b,c){var d=a+"?"+b;c?$c.sendBeacon&&$c.sendBeacon(d,c):pd(d)};var Eq=window,Fq=document,Gq=function(a){var b=Eq._gaUserP {snip}

We also receive as many errors from ga-4-react (the catch-block in the code snippet above).我们还从 ga-4-react(上面代码片段中的 catch 块)收到了同样多的错误。 We also tried using the analytics snippet with react-helmet , but had the same result.我们还尝试将分析片段与react-helmet一起使用,但结果相同。

This error seems to be generated by a number of browsers (Chrome, Opera, Safari, mobile browsers) on many platforms (Android, Windows 10, OS X) so we can't really pinpoint any specific route, platform, browser that's common with these users.此错误似乎是由许多平台(Android、Windows 10、OS X)上的许多浏览器(Chrome、Opera、Safari、移动浏览器)生成的,因此我们无法真正确定任何特定的路由、平台、浏览器与这些用户。

I also tried to replicate this with AdBlock, blocking trackers, using Do Not Track, but nothing.我也尝试用 AdBlock 复制这个,阻止跟踪器,使用 Do Not Track,但没有。

Ran into the same issue on our site.在我们的网站上遇到了同样的问题。 This issue appears to stem from navigator.sendBeacon being called out of context, similar to the comment from hackape.这个问题似乎源于 navigator.sendBeacon 被断章取义地调用,类似于来自 hackape 的评论。

For context, this article has some pretty good info on sendBeacon and what it does, including a description of an error like the one you saw.就上下文而言,本文有一些关于 sendBeacon 及其作用的非常好的信息,包括对您所看到的错误的描述。

https://xgwang.me/posts/you-may-not-know-beacon/ https://xgwang.me/posts/you-may-not-know-beacon/

In our case, we only saw Type Error: Illegal Invocation, but didn't see the other error message variants based on different browsers that they mentioned.在我们的案例中,我们只看到了 Type Error: Illegal Invocation,但没有看到他们提到的基于不同浏览器的其他错误消息变体。 In the end we ignored it in sentry for the moment.最后我们暂时在哨兵中忽略了它。

You can use this code example您可以使用此代码示例

import React from "react";
import ReactDOM from "react-dom";
import GA4React, { useGA4React } from "ga-4-react";

const ga4react = new GA4React("G-1JXXXXX");

function MyApp() {
  const ga = useGA4React();
  console.log(ga);

  return <div className="App">hi!</div>;
}

(async () => {
  await ga4react.initialize();

  ReactDOM.render(
    <React.StrictMode>
      <MyApp />
    </React.StrictMode>,
    document.getElementById("root")
  );
})();

You are using ga4.pageview(window.location.path);您正在使用ga4.pageview(window.location.path); but path is not a property of window.locationpath不是window.location的属性

Try using pathname instead:尝试改用pathname

ga4.pageview(window.location.pathname);

Use react-ga npm package for implementing Google Analytics with below code使用react-ga npm package 使用以下代码实现 Google Analytics

Create helper file, which will help you for DRY concept创建帮助文件,这将帮助您实现 DRY 概念

import ReactGA from 'react-ga';

/**
 * Initialize Google Analytics
 */
export const initializeGA = () => {
  ReactGA.initialize(process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_KEY, {
    debug: true
  });
};

/**
 *  Push Page Tracking on Google Analytics
 */
export const pageView = () => {
  ReactGA.pageview(window.location.pathname + window.location.search);
};

Then initialize Google Analytics on your root component然后在您的根组件上初始化 Google Analytics

import React, { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { initializeGA, pageView } from '../helpers/analytics';

export default function MyApp() {
  const location = useLocation();

  useEffect(() => {
    initializeGA();
  }, []);

  useEffect(() => {
    pageView();
  }, [location.pathname]);

  return (
    <div>
      <p>Your root component text</p>
    </div>
  );
}

If you are using old React versions it can be implemented using class component and lifecycle hooks componentDidMount , componentDidUpdate/componentWillRecieveProps based on your react version.如果你使用的是旧的 React 版本,它可以使用class component和生命周期钩子componentDidMountcomponentDidUpdate/componentWillRecieveProps根据你的反应版本来实现。 Sample for the same is.相同的样品是。

import { browserHistory } from 'react-router';

class App extends React.Component {
  componentDidMount() {
    this.unlisten = browserHistory.listen((location) => {
      console.log('route changes');
    });
  }
  componentWillUnmount() {
    this.unlisten();
  }
  render() {
    return (
      <div>
      </div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM