Introduction
This section provides comprehensive instructions on how to use callbacks for user actions and integrate them with any third-party or internal analytics tools.
Tracking Analytics Events
To track user interactions and events, the useAnalytics hook is available. Below is a detailed guide on how to set up and use this hook for tracking analytics in your application.
1. Setup: Import and Define the Event Payload
To use analytics tracking, first import the required modules and create a function to structure the event payload:
import gluedin from "gluedin-shorts-js";
import { getDeviceData, getUserData, getPageData, getCategoriesData, getUserFollowing } from "./helpers"; // Replace with actual imports
import { EVENTS } from "./constants"; // Replace with actual event constants
// Helper function to generate an event payload
async function getEventPayload(data: any) {
const { event, content } = data ?? {};
const { videoId: contentId, user: creator, hashtags, contentType, categoryId } = content ?? {};
const { userId: creatorUserId, userName: creatorUserName, followersCount } = creator ?? {};
let payload = {
eventName: event,
...getDeviceData(),
...getUserData(),
...getPageData(),
...(contentId && { content_id: contentId }),
};
const categories = await getCategoriesData();
const categoryData = categories.find((category: any) => category.categoryId === categoryId);
const { categoryName = "" } = categoryData ?? {};
// Event-specific payload adjustments
switch (event) {
case EVENTS.VIEW_IMPRESSION:
payload = {
...payload,
content_type: contentType,
creator_userid: creatorUserId,
creator_username: creatorUserName,
hashtag: hashtags,
};
break;
case EVENTS.CONTENT_STOP_PLAY:
payload = {
...payload,
content_played_duration: content.timeElapsed,
};
break;
case EVENTS.CONTENT_LIKE:
case EVENTS.CONTENT_UNLIKE:
const { isFollowing: followingCount } = await getUserFollowing(creatorUserId);
payload = {
...payload,
button_type: "NA",
content_type: contentType,
creator_userid: creatorUserId,
creator_username: creatorUserName,
failure_reason: "NA",
hashtag: hashtags,
success: true,
user_follower_count: followersCount,
user_following_count: followingCount,
user_isFollow: content.isFollow,
};
break;
case EVENTS.CONTENT_SHARE:
payload = {
...payload,
content_type: contentType,
content_genre: categoryName,
creator_userid: creatorUserId,
creator_username: creatorUserName,
hashtag: hashtags,
};
break;
case EVENTS.THUMBNAIL_CLICK:
payload = {
...payload,
content_type: contentType,
content_genre: categoryName,
vertical_index: content.verticalIndex,
horizontal_index: content.horizontalIndex,
};
break;
default:
break;
}
return payload;
}
2. Hook Implementation: useAnalytics
Define the useAnalytics hook for tracking events:
export default function useAnalytics() {
const trackEvent = async (data: any) => {
try {
const userModule = new gluedin.GluedInActivityTimeline();
const eventData = await getEventPayload(data);
const event = await userModule.activityTimelineAnalytics(eventData);
if (event.status === 200) {
console.log("Event tracked successfully");
}
} catch (error) {
console.error("Error tracking event:", error);
}
};
return { trackEvent };
}
3. Using trackEvent to Capture Events
Call the trackEvent function to log specific user interactions, such as video plays, likes, or views:
import useAnalytics from "./hooks/useAnalytics"; // Replace with the correct path
import { EVENTS } from "./constants"; // Replace with the correct path
const { trackEvent } = useAnalytics();
// Example usage for tracking video play event
trackEvent({
event: EVENTS.CONTENT_PLAY,
content: video, // Replace with the actual video object
});
Event Types and Usage
-
EVENTS.CONTENT_PLAY: Triggered when a video starts playing.
-
EVENTS.CONTENT_LIKE / EVENTS.CONTENT_UNLIKE: Triggered when a user likes or unlikes a video.
-
EVENTS.VIEW_IMPRESSION: Captures when a video or content is viewed.
-
EVENTS.CONTENT_SHARE: Captures when content is shared.
-
EVENTS.THUMBNAIL_CLICK: Captures when a thumbnail is clicked.
This guide should help you seamlessly integrate analytics tracking for user interactions in your application using GluedIn's SDK.