Integration with React
No doubt that, React is one of the most famous frontend frameworks nowadays. As Metricalp, we are happy to have an official integration library for React and NextJS. If you are using NextJS, please check NextJS integration docs. If you are using React, you are in the right place. Let's start!
Our integration library in NPM, which name is @metricalp/react
Installation
You can install it as:
yarn add @metricalp/react
or
npm install @metricalp/react
Integration
In your app's top level src/index.js | src/main.tsx(Vite) | App.js, wrap your app with MetricalpReactProvider. Then give your tid (a.k.a TrackerID) as prop to provider. Basically this is all, Metricalp will start to collect all visit events and route change events automatically. But you can customize behaviours with props. We will explain all.
Example (Vite bootstrapped React project)
In src/main.tsx import library import { MetricalpReactProvider } from '@metricalp/react';. Then wrap your app with <MetricalpReactProvider allowLocalhost tid="YOUR_TID">
DON'T FORGET to replace tid prop with your tid.
We added allowLocalhost because defaultly Metricalp does not work in localhost to protect your quota limits. We allowed localhost for testing purposes. You would want to remove it after testing it once to save your quota. For detailed info check allow localhost & exclude me docs.
Code example
import React from 'react';
import ReactDOM from 'react-dom/client';
import { MetricalpReactProvider } from '@metricalp/react';
import './index.css';
import { App } from './App.tsx';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<MetricalpReactProvider allowLocalhost tid="YOUR_TID">
<App />
</MetricalpReactProvider>
</React.StrictMode>
);
USAGE
If you integrated library with your app, it automatically starts to tracking screen_view events. Metricalp listens for window.history changes and creates screen_view event on every path change defaultly. So, if you want to track only screen views then your app is ready just after integration. You do not need to do anything extra. But if you want to produce custom events or disable auto route tracking and you will create screen_view events manually then follow this section.
Produce Events
You have two options to produce Metricalp events with this library. A hook or direct function call.
Event produce with Hook
import { useMetricalp } from '@metricalp/react';
export default function MyComponent() {
// Call hook to get the metricalp event function
const metricalpEvent = useMetricalp();
return (
<div>
<button
onClick={() => {
metricalpEvent({ type: 'click_test_button', custom_prop1: 'hook' });
}}>
Click me
</button>
</div>
);
}
Event produce with Direct function call
Sometimes (outside of components) you can not call hooks in React. Then you can import direct event trigger function from Metricalp.
import { metricalpEvent } from "@metricalp/react";
export default function MyComponent() {
return (
<div>
<button
onClick={() => {
metricalpEvent({ type: 'click_test_button', custom_prop1: 'direct' });
}}
>
Click me
</button>
</div>
);
}
It is totally up to you prefer hook or direct function call in your whole app. You can decide about usage of only one or you can use both depending usage place. In above examples we produce custom events with custom props. In your metricalp dashboard you can set custom_prop aliases then you can use directly that key in events. Like: metricalpEvent({ type: 'click_test_button', user_role: 'admin' }); here we defined user_role as alias of custom_prop1 in Tracker Settings and now we are producing it in events directly.
Customize behaviours with Props
tid
This is your tid a.k.a tracker id. You can get info about to getting it from Tracker Settings / Embed & Share. You need to provide this attribute to make Metricalp work.
allowLocalhost
If you are integrating Metricalp in your localhost for testing purposes, you can set this attribute to "true". Otherwise Metricalp will not collect events in localhost to save your usage quota. Check this docs for detailed info If you are allowing specific hostnames in your tracker settings, please also add localhost to allowed hostnames.
customScriptUrl
If you proxied Metricalp event script file to prevent adblockers, use this attribute to provide proxied script url. Check this docs for detailed info.
customEventEndpoint
If you proxied Metricalp event endpoint to prevent adblockers, use this attribute to provide proxied endpoint. Check this docs for detailed info.
disableAutoRouteCatch
Defaultly Metricalp listens for window.history events to catch route changes in JS applications. You can disable it with setting this attribute to true, then you can track routes manually.
hashRouting
If your application uses hash routing (example.com/#about, example.com/#homepage), you can set this attribute to true. Then Metricalp will listen for hash route changes.
allowCustomElmEvents
Metricalp defaultly supports HTML attribute based custom event tracking. Like <button data-metricalp-event="follow click" data-metricalp-user="John">Click</button>, this button click will create a custom event with name "follow click" and custom prop "user" with value "John". You can disable this feature with setting this attribute to false. In library it is disabled by default while collection custom events with API is more robust as described above. Check this docs for detailed info
initialSharedCustomProps
If you want to attach some custom props automatically and defaultly for all or some specific events then you can use initialSharedCustomProps prop.
<MetricalpReactProvider
allowLocalhost
tid="mam123456"
initialSharedCustomProps={{
_global: { custom_prop3: "v1.0" },
screen_view: { color: 'red', custom_prop2: 'Test' }
}}>
_global is special key for attaching some props to all events. Keep in mind that, if you are attaching a custom prop via alias, then that alias should be defined for target events in Tracker Settings otherwise it will be ignored for that event. You can use custom_prop1 like syntax for any event always.
When you want to update sharedCustomProps after initial setup for one specific event type, you can use exported method `updateSharedCustomPropsForType` like: updateSharedCustomPropsForType('screen_view', { color: 'red' }). Also, if you want to reset whole sharedCustomProps object to a new object, you can use exported method `resetSharedCustomProps` like: resetSharedCustomProps({ _global: { custom_prop3: 'v1.7' }, screen_view: { color: 'blue' } }). Check this docs for detailed info.