react 交互_如何在React应用程序中跟踪用户交互

react 交互

by Faouzi Oudouh

通过Faouzi Oudouh

如何在React应用程序中跟踪用户交互 (How to track user interactions in your React app)

Worry not about which Analytics provider you need to gather user interaction within your app.

不必担心需要哪个Analytics(分析)提供程序来收集应用程序内的用户交互。

Instead, worry more about how to gather these interactions.

相反,请更多地担心如何收集这些交互。

A few months ago, I was involved in an Analytics project within a large E-commerce organization. This organization has a data-driven business where the analytics are more important than anything else.

几个月前,我参与了一个大型电子商务组织中的Analytics项目。 该组织拥有数据驱动的业务,其中分析比其他任何事情都重要。

We were building a Datalayer solution to hold all the user interactions and actions before pushing them to the Analytics provider (for example, Google Tag Manager). We built our DataLayer solution without having React in mind, as the migration to React started later.

我们正在构建一个数据层解决方案,以保留所有用户交互和操作,然后再将其推送给Google Analytics(分析)提供商(例如Google跟踪代码管理器)。 我们在构建DataLayer解决方案时没有考虑到React,因为后来开始迁移到React。

React时间! (React Time!)

We started the migration to React progressively, which means React was responsible only for rendering some parts of the platform. And I was responsible for integrating the DataLayer solution we had already built with React Land.

我们开始逐步迁移到React,这意味着React仅负责渲染平台的某些部分。 我负责整合我们已经使用React Land构建的DataLayer解决方案。

Suddenly, the difficulties started coming up:

突然,困难开始出现:

  • The solution was jQuery based

    该解决方案基于jQuery
  • It was unpredictable

    这是不可预测的
  • It was hard to test and maintain

    很难测试和维护
  • Sharing knowledge with other developers who didn’t have analytics experience was scary!

    与没有分析经验的其他开发人员共享知识非常可怕!

I started looking in the community for ready-to-use solutions that fit our needs. There was just no chance.

我开始在社区中寻找适合我们需求的即用型解决方案。 只是没有机会。

And here’s where the idea of React-Tracker came in.

这就是React-Tracker的想法出现的地方。

Why React-tracker?

为什么选择React-tracker ?

  • It’s easy to use, test, and maintain (Redux-like)

    易于使用,测试和维护(类似于Redux)
  • It can be used with any Analytics provider

    可以与任何Google Analytics(分析)提供商一起使用
  • It’s scalable and predictible

    可扩展且可预测
  • It has a minimal API

    它有一个最小的API

With React-tracker, we were easily able to integrate two Analytics providers (Google Tag manager and Adobe Analytics).

借助React-tracker,我们可以轻松集成两个Google Analytics(分析)提供程序(Google跟踪代码管理器和Adobe Analytics)。

怎么样? (How?)

To keep it simple, think of it as Redux.

为简单起见将其视为Redux

  • Instantiate your Tracker ~ Store of your events

    实例化Tracker〜 事件存储

  • Create your event-listener(s) ~ Reducer

    创建您的事件监听器〜 减速器

  • Event ~ Action

    事件〜 动作

  • Provide your tracker instance to your Root Component.

    将跟踪器实例提供给根组件。
  • React-tracker will magically take care of providing your tracker instance to all your Components.

    React-tracker将神奇地为您的所有组件提供跟踪器实例。

Before instantiating anything, let’s go through each term on the list above and explain it.

在实例化任何内容之前,让我们仔细阅读上面列表中的每个术语并进行解释。

什么是追踪器? (What is Tracker?)

A Tracker is a bag that holds the tracking-history along with some functions to listen to/dispatch events.

跟踪器是一个装有跟踪历史记录以及一些侦听/调度事件的功能的包。

  • tracker.on(eventType, callback) the given callback will be called whenever an event with event.type equal to the given eventType is dispatched.

    每当调度event.type等于给定eventType的事件时,都会调用tracker.on(eventType, callback)给定的回调。

  • tracker.trackEvent(event) is a function that accepts an event and calls all the event-listeners that listen on this event.

    tracker.trackEvent(event)是一个接受event并调用侦听此event所有事件侦听器的函数。

  • tracker.getHistory() returns an Array and contains all the tracked events that were saved

    tracker.getHistory()返回一个数组,其中包含所有已保存的跟踪事件

什么是活动? (What is an Event?)

An event is a plain object that represents the user interaction, like user click, page view, and purchase.

事件是代表用户互动(例如用户点击,页面浏览和购买)的普通对象。

It should be an object with type and associated data if any. Here’s an example of a PageView event:

它应该是具有type和相关data如果有)的对象。 这是PageView事件的示例:

const PageViewEvent = {  type: 'PAGE_VIEW', // Required  data: { // Optional    pageId: '123',    userId: 'UID-123'  }}

什么是事件监听器? (What is the Event-listener?)

The event-listener is a function that will be called if its eventType matched the type of the dispatched event.

event-listener是一个函数,如果其eventType与调度的事件的类型匹配,则将调用该函数。

eventListener.eventType === event.type

eventListener.eventType === event.type

Example of an Event-listener:

事件侦听器的示例:

const pageViewListener = (event, ) => {  // For example let's push the received event to our DataLayer.  window.dataLayer.push(event);
return event;};

Let’s allow our pageViewListener to listen only on PAGE_VIEW event:

让我们让pageViewListener仅监听PAGE_VIEW事件:

pageViewListener.eventType = 'PAGE_VIEW';

There are four things to notice here:

这里有四件事要注意:

  • Returning the event will save it in the trackingHistory. Otherwise it will be ignored :)

    返回事件将其保存在trackingHistory中。 否则它将被忽略:)
  • If no eventType was specified to the event-listener, it will be called on every event dispatch.

    如果未为事件侦听器指定eventType ,则将在每次事件分派时调用它。

  • eventHistory was provided as a second parameter to help you apply restrictions on your events easily, like tracking a Product-click once. In order to achieve this, you need to have the history of events in your hands.

    提供了eventHistory作为第二个参数,以帮助您轻松地对事件应用限制,例如跟踪一次产品点击。 为了实现这一点,您需要掌握事件的历史记录。

  • Pushing our event to window.dataLayer was just an example. You can mainly do anything in this function like calling GTM directly or Facebook Pixel

    将我们的事件推送到window.dataLayer只是一个示例。 您主要可以使用此功能执行任何操作,例如直接调用GTMFacebook Pixel

是时候结合一切了 (Time to combine everything)

First things first:

第一件事:

1.实例化我们的英雄Tracker: (1. Instantiate our hero Tracker:)

import { Tracker } from 'react-tracker';
const tracker = new Tracker();

That’s it!

而已!

Now we have our Tracker but with no event-listener :-(

现在我们有了Tracker但是没有事件监听器:-(

There are two ways to add event-listeners to your Tracker :

有两种方法可以将事件监听Tracker添加到Tracker

  • On instantiating:

    实例化时:
const anOtherTracker = new Tracker([  pageViewListener,  productClickListener,  ...]);
  • Or you can add the event-listener after instantiating your Tracker using on:

    或者,您可以使用on实例化Tracker后添加事件监听Tracker

const anOtherTracker = new Tracker();
tracker.on('PAGE_VIEW', pageViewListener);

2.创建一个页面视图事件侦听器: (2. Create a page view event-listener :)

I want my event-listener to push the received PAGE_VIEW event directly to my dataLayer.

我希望事件监听器将接收到的PAGE_VIEW事件直接推送到我的dataLayer.

const pageViewListener = (event, trackingHistory) {
window.dataLayer.push(event);
};

Let our tracker know about the pageViewListener :

让我们的tracker了解pageViewListener

tracker.on('PAGE_VIEW', pageViewListener);

3.创建事件创建者: (3. Create Event-creator :)

Event-creator is just a function that returns an event object:

事件创建者只是一个返回事件对象的函数:

const pageViewEvent = (pageId, userId) => ({  type: 'PAGE_VIEW',  data: {    pageId,    userId  }});

Our Tracker is well configured now.

我们的Tracker现在配置正确。

向我们的tracker介绍React (Introducing our tracker to React)

4.向根组件提供我们的tracker(4. Provide our tracker to the Root Component:)

import React from 'react;import ReactDOM from 'react-dom';import { TrackerProvider } from 'react-tracker'
import RootComponent from '../RootComponent';
const RootComponentWithTracking = (  <TrackerProvider tracker={tracker}>    <RootComponent />  </TrackerProvider>);
const domElement = document.getElementById('root');
ReactDOM.render(<RootComponentWithTracking />, domElement);

By providing our tracker to the root component, it will be magically available for all the sub-components.

通过为根组件提供tracker ,它可以神奇地用于所有子组件。

So now, since we have our tracker available, let’s use it to track the PAGE_VIEW event on the RootComponent mount.

所以,现在,因为我们有我们的tracker可用,让我们用它来跟踪PAGE_VIEW上的事件RootComponent安装。

4.跟踪Page View Event. (4. Track Page View Event.)

import React from 'react';import { withTracking } from 'react-tracker';// We created this function earlier at (3.)import { pageViewEvent} from '../tracking/events';
class RootComponent extends React.Component {  componentDidMount() {    this.props.trackPageView(this.props.pageId, this.props.userId)  }
render() {    return (<h1> My App is awesome </h1>)  }};
const mapTrackingToProps = trackEvent => ({  trackPageView: (pageId, userId) =>     trackEvent(pageViewEvent(pageId, userId))});
export default withTracking(mapTrackingToProps)(RootComponent);

withTracking HOC will take care of providing us trackEvent from our tracker so we can use it to track the pageView event.

withTracking HOC将trackEventtracker向我们提供trackEvent ,以便我们可以使用它来跟踪pageView事件。

mapTrackingToProps will merge the returned object with the RootComponent ’s props, which means the trackPageView will be available as a prop within RootComponent.

mapTrackingToProps将合并返回的对象与RootComponent的道具,这意味着trackPageView将在RootComponent.作为道具RootComponent.

That’s it — you’re done ;)

就是这样-您完成了;)

5.演示 (5. Demo)

Please refer to this demo and to GitHub for in-depth documentation and a better way to organize your tracking files.

请参阅此演示和GitHub ,以获取深入的文档以及更好的组织跟踪文件的方法。

试试看! (Give it a try!)

React-tracker was built to facilitate the integration of Analytics tools as much as possible, by proving a minimal API and easy integration with your react app.

React-tracker的构建是通过证明最少的API并轻松与您的react应用程序集成来尽可能地促进Analytics工具的集成。

谢谢 (Thanks)

Thank you doha faridi, AbdelAli Eramli and khalid benrafik for your helpful feedback.

感谢doha faridi , AbdelAli Eramli和khalid benrafik的有用反馈。

翻译自: https://www.freecodecamp.org/news/how-to-track-user-interactions-in-your-react-app-b82f0bc4c7ff/

react 交互

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394510.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

shell python比较_shell中的条件判断以及与python中的对比

shell中比如比较字符串、判断文件是否存在及是否可读等&#xff0c;通常用"[]"来表示条件测试。注意&#xff1a;这里的空格很重要。要确保方括号的空格。if ....; then python中的条件判断&#xff1a; if ....: (此处是冒号&#xff0c;不同…

服务器麒麟系统能设置mtu吗,麒麟操作系统安装标准手册-20210405220006.docx-原创力文档...

精品文档精品文档PAGEPAGE47精品文档PAGE.银河麒麟V3操作系统安装手册V1.2编制&#xff1a;王帅校核&#xff1a;朱本亮审定&#xff1a;周俊...文档更新日志&#xff1a;序号修订时间修订内容修改人审定人012017-04-12发布文档V1.0王帅周俊022017-05-11增加启动安装时蓝屏错误…

多个 gradle 文件夹 \.gradle\wrapper\dists\ 设置gradle不是每次都下载

韩梦飞沙 韩亚飞 313134555qq.com yue31313 han_meng_fei_sha 设置gradle不是每次都下载 \.gradle\wrapper\dists\ 在你导入项目的时候&#xff0c;有个选项的&#xff1a; 你要是选了Use default gradle mapper就会下载一次&#xff0c;Use local gradle distribution就会…

docker使用方式

docker使用方式安装&#xff1a;1.安装依赖 yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 2添加yum源 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo 3.安装 yum install docker-ce docker-ce-cli contain…

使用AxiosJavaScript中的简单HTTP请求

Interested in learning JavaScript? Get my ebook at jshandbook.com有兴趣学习JavaScript吗&#xff1f; 在jshandbook.com上获取我的电子书 介绍 (Introduction) Axios is a very popular JavaScript library you can use to perform HTTP requests. It works in both Brow…

Linux中通过命令直接删除文件中最后一行

何谓Sed(Stream EDitor):Sed原为UNIX系统上的非交谈式文字编辑器(non-interactive stream editor)。当Sed读入待编辑文件&#xff0c;会依编辑命令来进行文件的编辑工作。sed -i $d file如同其它UNIX的指令一般&#xff0c;Sed亦是由标准输入(standard input)读入欲编辑的文件&…

vb获取数组长度_如何实现数组的二分查找

二分查找是一种极其高效、简练的查找算法&#xff0c;它不仅简单&#xff0c;易用&#xff0c;而且还非常的高效。相对于顺序查找&#xff0c;二分查找在效率是呈现指数性提升&#xff0c;数据量越大&#xff0c;越能体现出二分查找法的优势。二分查找的查找过程是&#xff1a;…

400错误返回了服务器信息,使用Spring MVC,接受JSON错误的POST请求会导致返回默认的400错误代码服务器页面...

我正在使用RESTAPI。接收到带有错误JSON的POST消息(例如{sdfasdfasdf})会使Spring返回默认服务器页面&#xff0c;以显示400错误请求错误。我不想返回页面&#xff0c;我想返回自定义JSON错误对象。当使用ExceptionHandler引发异常时&#xff0c;可以执行此操作。因此&#xff…

【Python】list和tuple 区别比较

列表 List classmates [Michael, Bob, Tracy] 元组 Tuple tuple一旦初始化就不能修改&#xff0c;比如同样是列出同学的名字&#xff1a; >>> classmates (Michael, Bob, Tracy) 现在&#xff0c;classmates这个tuple不能变了&#xff0c;它也没有append()&#xff…

leetcode315. 计算右侧小于当前元素的个数(树状数组解法)

leetcode315. 计算右侧小于当前元素的个数(树状数组解法) 题目&#xff1a;给定一个整数数组 nums&#xff0c;按要求返回一个新数组 counts。数组 counts 有该性质&#xff1a; counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。 树状数组解法 java class Solution …

洛谷 P1101 单词方阵

给一nn的字母方阵&#xff0c;内可能蕴含多个“yizhong”单词。单词在方阵中是沿着同一方向连续摆放的。摆放可沿着 8个方向的任一方向&#xff0c;同一单词摆放时不再改变方向&#xff0c;单词与单词之间可以交叉,因此有可能共用字母。输出时&#xff0c;将不是单词的字母用*代…

从头学习计算机网络_如何从头开始构建三层神经网络

从头学习计算机网络by Daphne Cornelisse达芙妮康妮莉丝(Daphne Cornelisse) 如何从头开始构建三层神经网络 (How to build a three-layer neural network from scratch) In this post, I will go through the steps required for building a three layer neural network. I’…

python 文件处理

f open(chenli.txt) #打开文件 first_line f.readline() print(first line:,first_line) #读一行 print(我是分隔线.center(50,-)) data f.read() # 读取剩下的所有内容,文件大时不要用 print(data) #打印读取内容f.close() #关闭文件1…

第五章 MVC之Bundle详解

一、简述 Bundle&#xff0c;英文原意就是捆、收集、归拢。在MVC中的Bundle技术&#xff0c;也就是一个对css和js文件的捆绑压缩的技术。 它的用处&#xff1a; 将多个请求捆绑为一个请求&#xff0c;减少服务器请求数 压缩javascript&#xff0c;css等资源文件&#xff0c;减小…

所给服务器端程序改写为能够同时响应多个客户端连接请求的服务器程序_一文读懂客户端请求是如何到达服务器的...

点击上方“蓝色字体”&#xff0c;选择 “设为星标”关键讯息&#xff0c;D1时间送达&#xff01;互联网是人类历史上最伟大的发明创造之一&#xff0c;而构成互联网架构的核心在于TCP/IP协议。那么TCP/IP是如何工作的呢&#xff0c;我们先从数据包开始讲起。1、数据包一、HTTP…

消息服务器 推送技术,SSE服务器推送技术

SSE即 server send event 服务器发送事件&#xff0c;在在早期可能会使用ajax向服务器轮询的方式&#xff0c;使浏览器第一时间接受到服务器的消息&#xff0c;但这种频率不好控制&#xff0c;消耗也比较大。但是对于SSE来说&#xff0c;当客户端向服务端发送请求&#xff0c;服…

Contest2162 - 2019-3-28 高一noip基础知识点 测试5 题解版

传送门 T1 单调栈 按照b排序 在家每一个物品时&#xff0c;判断一下a和b的关系 如果s[sta[top]].a>s[i].b&#xff0c;就弹栈 记录所有时候的height&#xff0c;并取最大值 T2 单调栈裸题 单调栈是干什么的&#xff1f;&#xff1f; 单调栈是记录一个数的一侧的第一个比他大…

在package.json里面的script设置环境变量,区分开发及生产环境。注意mac与windows的设置方式不一样...

在package.json里面的script设置环境变量&#xff0c;区分开发及生产环境。 注意mac与windows的设置方式不一样。 "scripts": {"publish-mac": "export NODE_ENVprod&&webpack -p --progress --colors","publish-win": "…

leetcode 978. 最长湍流子数组(动态规划)

978. 最长湍流子数组 当 A 的子数组 A[i], A[i1], …, A[j] 满足下列条件时&#xff0c;我们称其为湍流子数组&#xff1a; 若 i < k < j&#xff0c;当 k 为奇数时&#xff0c; A[k] > A[k1]&#xff0c;且当 k 为偶数时&#xff0c;A[k] < A[k1]&#xff1b; 或 …

人工智能取代工作_人工智能正在取代人们的工作-开发人员是下一个吗?

人工智能取代工作I was recently asked to comment on whether there was any point in becoming a developer right now, because AI might be doing your job very soon.最近有人要求我评论一下现在成为开发人员是否有任何意义&#xff0c;因为AI可能很快就会完成您的工作。 …