use-tracker
v0.1.0
Published
A React Hook to easily add Google Analytics tracking for SPA apps using React Router
Downloads
4
Maintainers
Readme
use-tracker
Easily add google analytics tracking (https://github.com/react-ga/react-ga) to your React pages
Installation
npm install --save use-tracker
Initializing - done at the top level before any Routing is done
import { init } from ' use-tracker';
// Use your GA tracking ID
init('UA-12345', process.env.NODE_ENV || 'production');
Importing the hook
import useTracker from ' use-tracker'
Usage
use-tracker is powered by React Google Analytics in the background. Simply use the hook and provide a pathname and/or search string, and thats it.
import React from 'react';
import useTracker from 'use-tracker';
function YourPageComponent() {
useTracker({
pathname: 'stuff',
search: '?and=things'
});
return (
...
)
}
It works perfectly with React Router
import React from 'react';
import useTracker from 'use-tracker';
function YourPageComponent({ location }) {
useTracker({
pathname: location.pathname
search: location.search
});
return (
...
)
}