24
loading...
This website collects cookies to deliver better user experience
product / marketing
: this tracking goals is to keep track and evaluate marketing approaches (FB ads, google ads, instagram link, etc), and help product team to evaluate UXerror
: this tracking purpose is to notify developer about the error that occur in production before customer making any complain.Home
and Top News
refresh
news functionalitygo to source
button, we want to evaluate whether user usually go to tops news
tab or not, so the Data expected looks like :
{
eventName: 'click_go_to_source',
page: 'Home / TopNews'
}
refresh feed
button, we want to evaluate whether user click refresh feed
button or not so the data expected looks like :
{
eventName: 'refresh_feed',
page: 'Home / TopNews'
}
fetching data
, we want to track every error occur when fetching data. Data expect to looks like :
{
eventName: 'error_fetch',
page: 'Home / TopNews',
errorReason: stringify error object
}
DataDog
for our error tracking and MixPanel
for our click tracking. go to source
this code will send over the data to mock MixPanel
.// ArticleCard.js
...
// line 7
const handleClick = () => {
const eventName = "click_go_to_source";
const unique_id = uuid();
MixPanel.track(eventName, unique_id, {
page,
});
...
};
....
refresh feed
this code will send over the data to mock MixPanel
.// Home.js or TopNews.js
...
// line 26
const onRefreshClick = () => {
const eventName = "refresh_feed";
const unique_id = uuid();
MixPanel.track(eventName, unique_id, {
page,
});
...
};
....
fetch_error
data to mock DDlog
.// Home.js or TopNews.js
...
// line 15
onError: (err) => {
const eventName = "error_fetch";
DDlog.error(eventName, {
page,
errorReason: JSON.stringify(err, null, 4),
});
},
....
MixPanel
to Heap
. we need to manually refactor all of our MixPanel
tracking code 1-by-1 😵💫. react-tracking
by NYT, a React specific tracking library. it helps to :ReactTrackingInitializer
HOC (High Order Component) to be our parent / root tracking wrapper.const ReactTrackingInitializer = ({ children }) => {
const { Track } = useTracking(
{
// this is where the initialize data put
trackVersion: "1.0.0",
},
{
dispatch: (trackedData) => {
console.log("dispatchData", trackedData);
}
);
return <Track>{children}</Track>;
};
useTracking
is a hooks version to implementing react-tracking
which suitable for functional component, find out more on their docs if you still implementing class component.useTracking
takes 2 params:dispatch
,dispatchOnMount
,process
, and fowardRef
more detail check react-tracking
useTracking
will return object with 3 properties:trackEvent
: a function to send data to be process at process
, then dispatch
.getTrackingData
: a function that return current initial data in our tracker.Track
: a HOC that wrapped a child component to give scope to it's initial data, process
and dispatch
logic. which later can be triggered using trackEvent
dispatch
option. so it will looks like this :...
dispatch: (trackedData) => {
console.log("dispatchData", trackedData);
const { eventName, ...restOfData } = trackedData.data;
switch (trackedData.type) {
case "product":
const unique_id = uuid();
MixPanel.track(eventName, unique_id, restOfData);
break;
case "error":
DDlog.error(eventName, restOfData);
break;
default:
break;
}
},
...
redux
reducers. Now you might ask there must be a dispatch mechanism to like redux, where is it ? checkout the code at Home.js
line 25 - 33const { trackEvent, Track } = useTracking({
data: { page: "HOME" },
});
const onRefreshClick = () => {
trackEvent({ type: "product", data: { eventName: "refresh_feed" } });
refetch();
};
trackEvent
will send over the data below to our dispatch
function.{
type: "product",
data: {
eventName: "refresh_feed",
page: "HOME"
}
trackVersion: "1.0.0"
}
trackVersion: "1.0.0"
and page: "HOME"
came from 🙄 ? react tracking perform a merge operation on data we sent and initial data provided. in this case :{
type: "product",
data: {
eventName: "refresh_feed"
}
}
Home.js
useTracking :
{
data: {
page: "HOME"
}
}
ReactTrackingInitializer
useTracking:
{
trackVersion: "1.0.0"
}
react-tracking
🎉🎉🎉, Just Note that:<Track></Track>
at root level (prefer to wrap )<Track></Track>
. that why we wrapped <ArticleCard>
in Home.js
line 57 - 63, so it get the initial value from Home.js
useTracking, otherwise it will only have initial value of ReactTrackingInitializer.js
.rtracking
and rtracking-solution
. Changes need to solve the problem statement:
direct
and direct-solution`. Changes need to solve the problem statement:
change MixPanel to Heap add user data, because we have add login feature