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.pswp.cn/news/394510.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394510.shtml
英文地址,請注明出處:http://en.pswp.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可能很快就會完成您的工作。 …