All Projects → faouzioudouh → React Tracker

faouzioudouh / React Tracker

React specific tracking library, Track user interaction with minimal API!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Tracker

Keen Tracking.js
A light, fast and flexible javascript tracking library
Stars: ✭ 218 (+14.14%)
Mutual labels:  analytics, tracking, tracker
Aurelia Google Analytics
An Aurelia.io plugin that adds Google Analytics page tracking to your project.
Stars: ✭ 28 (-85.34%)
Mutual labels:  analytics, tracking, google-analytics
Gumshoe
A we analytics and event tracking sleuth JavaScript library
Stars: ✭ 39 (-79.58%)
Mutual labels:  analytics, tracking, google-analytics
Vue Analytics
Google Analytics plugin for Vue
Stars: ✭ 1,780 (+831.94%)
Mutual labels:  analytics, tracking, google-analytics
svelte-google-analytics
Google Analytics component for Svelte
Stars: ✭ 41 (-78.53%)
Mutual labels:  tracking, analytics, google-analytics
Blacklist
Curated and well-maintained hostfile to block ads, tracking, cryptomining, and more! Updated regularly. ⚡🔒
Stars: ✭ 492 (+157.59%)
Mutual labels:  analytics, tracking, tracker
Vue Gtag
Global Site Tag plugin for Vue (gtag.js)
Stars: ✭ 445 (+132.98%)
Mutual labels:  analytics, tracking, google-analytics
Php Ga Measurement Protocol
Send data to Google Analytics from the server using PHP. Implements GA measurement protocol.
Stars: ✭ 561 (+193.72%)
Mutual labels:  analytics, tracking, google-analytics
Analytics
Simple, open-source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics.
Stars: ✭ 9,469 (+4857.59%)
Mutual labels:  analytics, google-analytics
Angulartics2
Vendor-agnostic analytics for Angular2 applications.
Stars: ✭ 963 (+404.19%)
Mutual labels:  analytics, google-analytics
Goaccess
GoAccess is a real-time web log analyzer and interactive viewer that runs in a terminal in *nix systems or through your browser.
Stars: ✭ 14,096 (+7280.1%)
Mutual labels:  analytics, google-analytics
Fathom
Fathom Lite. Simple, privacy-focused website analytics. Built with Golang & Preact.
Stars: ✭ 6,989 (+3559.16%)
Mutual labels:  analytics, google-analytics
Mintable
🍃 Automate your personal finances – for free, with no ads, and no data collection.
Stars: ✭ 849 (+344.5%)
Mutual labels:  analytics, tracker
Mixpanel Android
Official Mixpanel Android SDK
Stars: ✭ 907 (+374.87%)
Mutual labels:  analytics, tracking
App
Just a little analytics insight for your personal or indie project
Stars: ✭ 40 (-79.06%)
Mutual labels:  analytics, google-analytics
Analytics Spammers
Ce dépôt à pour but de bâtir un dictionnaire Open Source des spammers Analytics
Stars: ✭ 6 (-96.86%)
Mutual labels:  analytics, google-analytics
React Spy
A set of utilities for collecting UX-analytics of your React-application (ex: clicks, shows, errors and etc.)
Stars: ✭ 37 (-80.63%)
Mutual labels:  analytics, google-analytics
Google Analytics Api Symfony
Google Analytics API Symfony Bundle
Stars: ✭ 43 (-77.49%)
Mutual labels:  analytics, google-analytics
Block
Let's make an annoyance free, better open internet, altogether!
Stars: ✭ 1,849 (+868.06%)
Mutual labels:  tracking, tracker
Komito
🔖 Komito Analytics is a free, open-source enhancement for the most popular web analytics software.
Stars: ✭ 148 (-22.51%)
Mutual labels:  analytics, google-analytics

React tracker Logo

react-tracker build status npm version codecov

Track user interaction with minimal API

What?

  • React specific tracking library, usable as a higher-order component
  • Flexible-scalable solution for tracking
  • Can be pluged with any Analytics platform :
    • Google Tag Manager
    • Facebook Pixel
    • You can mainly do anything in the event listeners callback..
  • Easy to use (Redux-like)

Installation

$ npm install --save react-tracker

This assumes you are using npm as your package manager.

Demo

To see the react-tracker in action please visit the link below.

Link

Documentation

Initialize the Tracker

Create a Tracker holding the tracked events History of your app.

Tracker API is { on, trackEvent, getHistory }.

  • You can pass your already defined event listeners to the constructor like so:
const tracker = new Tracker([trackAddToCart])
  • Or you can listen-on-the-go using on():
const tracker = new Tracker();

// Listen on `PRODUCT_CLICK`event.
tracker.on('PRODUCT_CLICK', (event, eventsHistory) =>
  console.log(event)
);

// Listen on all events
tracker.on('*', (event, eventsHistory) =>
  console.log(event)
);

Create Event listener

Event listner is a pure function with (event, eventsHistory) => tracking goes here.

It describes what to do with the just-fired event.

Why providing the eventsHistory as second parameter ?

=> because in some cases you'll need to apply some restrictions on some events E.g:

  • Track product click only once!
  • Track product click only if pageView is already tracked
  • etc

Listen on one event

/**
 * Listener with eventType specified it will be called when the given eventType is dispatched
 */
function trackAddToCart(event, eventsHistory) {
    // Call DataLayer or your tracking provider (E.g. Pixel, GTM..)
    window.dataLayer.push(...event.data);

    // If you want save this event in the events history, just return it
    // otherwise it will be ignored.
    return event
}

// Allow `trackAddToCart` to listen only on `ADD_TO_CART` event
trackAddToCart.eventType = 'ADD_TO_CART';

Listen on all events

/**
 * Since no eventType was specified it will be called whenever an event dispatched
 * You can use `switch` statement to handle multiple events in one listener
 */
function trackCartEvents(event, eventsHistory) {
    switch(event.type) {
      case 'ADD_TO_CART':
        // Call DataLayer or your tracking provider (E.g. Pixel, GTM..)
        window.dataLayer.push(...event);
        break;

      default:
        // Silence
    }
}

Track Events

trackEvent is a function that accept an object describes the event as argument.

  • Track event ADD_TO_CART_EVENT with data.
tracker.trackEvent({
  type: 'ADD_TO_CART_EVENT',
  data: {
    productId: '12345',
    quantity: 5
  }
})
  • Track event PRODUCT_CLICK with no associated data.
tracker.trackEvent({ type: 'PRODUCT_CLICK' })

Usage with React

Provide tracker to the root component

All container components need access to the tracker so they can track events.

We will use the <TrackerProvider> to magically make the tracker available to all container components in the application without passing it explicitly.

You only need to use it once when you render the root component:

index.js

import React from 'react'
import { render } from 'react-dom'
import { TrackerProvider, Tracker } from 'react-tracker'
import { trackProductClick } from './tracking/listeners/cart'
import ProductsList from './components/ProductsList'

const tracker = new Tracker([trackProductClick])

render(
  <TrackerProvider tracker={tracker}>
    <ProductsList products={someProducts} />
  </TrackerProvider>,
  document.getElementById('root')
)

Create Add to Cart Event Listener .../tracking/listeners/cart.js

function trackAddToCart(event, eventsHistory) {
    window.dataLayer.push(...event);
    return event
}

// Allow `trackAddToCart` to listen only on `ADD_TO_CART` event
trackAddToCart.eventType = 'ADD_TO_CART';

export default trackAddToCart;

Add To Cart Event creator .../tracking/events/cart.js

Event creator should return an object that describe the event (Type and data).

  • type: string (Required)
  • data: Any (Optional)
function getAddToCartEvent(id, price) {
  return {
      type: 'ADD_TO_CART',
      data: {
          id: id,
          price: price
      }
  }
};

Create Product Component components/Product.js

import React from 'react'

const Product = ({ onClick, title, price, currency }) => (
  <li
    onClick={onClick}
  >
    {title}
    <span> {price} {currency} </span>
  </li>
)

export default Product

Create Products List Component components/ProductList.js

import Product from './Product'

const ProductList = ({ products, trackAddToCart }) => (
  <ul>
    {products.map(product => (
      <Product key={product.id} {...product} onClick={() => trackAddToCart(product.id, product.price)} />
    ))}
  </ul>
)

ProductList.propTypes = {
  // ...
  trackAddToCart: PropTypes.func
}

export default ProductList

Create mapTrackingToProps function and pass it to withTracking HOC .../compoenets/ProductListContainer.js

mapTrackingToProps should return an object which will be merged with the component Props.

import React from 'react';
import { withTracking } from 'react-tracker';
import { getAddToCartEvent } from '.../tracking/events/cart';
import ProductsList from './ProductsList';

const mapTrackingToProps = trackEvent => {
  return {
    trackAddToCart: (id, price) => {
      trackEvent(getAddToCartEvent(id, price))
    }
  }
}

// Finally, we create the `ProductsList` by calling `withTracking()` and passing our `mapTrackingToProps`
const ProductsListWithTracking = withTracking(mapTrackingToProps)(ProductsList)

export default ProductsListWithTracking

Create redux middleware for redux-based apps

If your app is using redux for state managment, you might want to track redux actions directly.

Let's create our Redux middleware to take the tracker as argument and call trackEvent on every redux action dispatched.

/**
 * Simple redux middleware to use redux actions as input of tracking!
 * this will call the track function from the provided instance of tracker on every action
 * and use the action type as the event type and the action payload as the event data
 * @param {Object} tracker 
 */
const trackingMiddleware = tracker => () => next => action => {
    tracker.trackEvent(action);
    next(action);
};

export default trackingMiddleware;
import { createStore, applyMiddleware } from 'redux';
import { Tracker } from 'react-redux';
import { trackingMiddleware, Tracker } from '../trackingMiddleware'

const tracker = new Tracker();

const store =  createStore(
  reducers,
  {}, // initialState
  applyMiddleware(trackingMiddleware(tracker))
);

// That's All ;)

Contribution

This project is in its early stages, I'd be very happy if you can help :)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].