React-GA事件在调试控制台中触发,但未在GA中显示 [英] React-GA events are firing off in debug console but not showing up in GA

查看:123
本文介绍了React-GA事件在调试控制台中触发,但未在GA中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在努力使这项工作正常进行,但没有任何进展.无论我做什么,都无法捕捉到这些事件.

I have been trying to make this work for a few days, and nothing is working. The events are simply not being captured no matter what I do.

我在顶部导入ReactGA

I import ReactGA at the top

import ReactGA from "react-ga";

我在导入后将其初始化

ReactGA.initialize("TRACKING-ID", {
  debug: true
});

我创建了一个处理跟踪器的功能

I create a function to handle the tracker

const GASignInHandler = name => {
  ReactGA.event({
    category: "Dashboard Home",
    action: `User Signing In As ${name}`,
    label: "Tracking users every time they sign in.",
    nonInteraction: true
  });
};

在我的名为DashHome的功能组件内部,并在返回中调用此函数.如果signedIn = true,则几乎可以运行此功能;如果不运行console.log,则可以运行此功能.显然,这是出于测试目的,但我仍未在GA中记录此事件.它会在调试时触发.

Inside my functional component called DashHome, and inside the return I call this. Pretty much if signedIn = true, run this function, if not run the console.log. This is for testing purposes obviously but I still don't see this event being logged in GA. It fires off in debug.

{signedIn ? GASignInHandler(userEmail) : console.log("signin not true")}

[react-ga]与fieldObject:{"hitType":事件","eventCategory":仪表板主页","eventAction":用户以我的身份登录","eventLabel":跟踪用户每次登录.," nonInteraction:true} log.js:2[react-ga]具有跟踪器:未定义

[react-ga] with fieldObject: {"hitType":"event","eventCategory":"Dashboard Home","eventAction":"User Signing in as My","eventLabel":"Tracking Users Every Time They Sign In.","nonInteraction":true} log.js:2 [react-ga] with trackers: undefined

我还将GA告诉我的所有标签都放入了public/index.html.

I also have all the tags that GA told me to put into my public/index.html.

推荐答案

将其放入index.js中即可解决此问题.

Putting this into index.js fixed the problem.

(function initAnalytics() {
  initGA("Tracking ID");
})();

initGA函数是手动创建的,因此(此文件称为index.js)

The initGA function is manually created and is as such (this file is called index.js)

import ReactGA from "react-ga";

export const initGA = trackingID => {
  ReactGA.initialize(trackingID);
};

export const PageView = () => {
  ReactGA.pageview(window.location.pathname + window.location.search);
};

export const Event = (category, action, label) => {
  ReactGA.event({
    category,
    action,
    label
  });
};

我还删除了我在public/index.html中拥有的所有脚本.我只有react-ga init.

I have also removed all the scripts that I had in public/index.html. I only have react-ga init.

似乎您也必须放上

ReactGA事件

ReactGA events

如果使用功能组件,请插入useEffect挂钩.

into a useEffect hook if youre using functional components.

这篇关于React-GA事件在调试控制台中触发,但未在GA中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆