Monetate Service API Usage
Introduction
This document provides an overview of the Monetate Service API implementation, including key methods for tracking events, handling API requests, and managing recommendations. refer monetate.service.ts for more details.
Monetate Service Class
The MonetateService class handles interactions with the Monetate API.
Calling the Monetate Engine API
This is the main method that handles the communication with the Monetate Engine API. It takes an array of MonetateEvent objects and an optional boolean parameter isDescisionRequest to determine whether to include a decision request event in the request.
async callEngineApi(event: MonetateEvent[], isDescisionRequest = true): Promise<ApiResponse> {
const uuid = useRandomUUID();
const monetateId = useCookie("mt.v").value;
let param = {
channel: "a-a77f9577/p/toolstation.nl",
monetateId: monetateId,
events: [...event],
};
if (isDescisionRequest) {
param.events.push({
eventType: "monetate:decision:DecisionRequest",
requestId: uuid,
includeReporting: false,
});
}
const response = await useAjax<ApiResponse>(
"https://engine.monetate.net/api/engine/v1/decide/toolstation",
{
method: "post",
params: param,
}
);
return response as ApiResponse;
}
Fetching Recommendations
Each event/ page-view request sent to monetate, we get some recommendations in response they are based on event type as well as page type that we are sending in requests
Here for storing rectokens (tokens associated with each prouduct id used by moenate for tracking) & other details are stored in pinia stores
async productDetailViewEvent(id: string) {
const monetateStore = useMonetateStore();
monetateStore.social_proof_data = ''
// Constructing the event object for the Product Detail View
const eventObject = [
{
eventType: "monetate:context:ProductDetailView",
pageType: 'product',
products: [
{
sku: id,
productId: id,
},
],
},
];
try {
// Making the API call
const response = await this.callEngineApi(eventObject);
// Extracting social proof data
const socialProof = response?.data?.responses[0]?.actions.find(
(action) => action.actionType === "monetate:action:SocialProofData"
);
if (socialProof) {
const count = socialProof.data?.count || 0;
const lookbackMinutes = socialProof.data?.lookbackMinutes || 0;
const days = Math.ceil(lookbackMinutes / (60 * 24));
const message = `${count} ${useTranslation('PDP-boughtInLast',"gekocht in de afgelopen")} ${days} ${days > 1 ? useTranslation("PDP-dagen",'dagen') :useTranslation("PDP-dag",'dag')}`;
monetateStore.social_proof_data = message
}
// Filtering actions for omnichannel recommendations
const actions = this.filterActions(response, {
actionType: "monetate:action:OmnichannelRecommendation",
});
// Extracting recommendations for "others also looked at"
const pdpOthersIds = extractItemsByComponent(actions, "pdp-others");
const pdpOthersRecTokens = extractAllRecTokens(actions);
monetateStore.pdp_others_also_looked_at = {
ids: pdpOthersIds,
recTokens: pdpOthersRecTokens,
};
// Extracting recommendations for "everything for your job"
const pdpEverythingIds = extractItemsByComponent(actions, "pdp-everything");
const pdpEverythingRecTokens = extractAllRecTokens(actions);
monetateStore.pdp_everything_for_your_job = {
ids: pdpEverythingIds,
recTokens: pdpEverythingRecTokens,
};
// Extracting recommendations for "everything for your job"
const pdpSimilarItemIds = extractItemsByComponent(actions, "pdp-similar-items");
const pdpSimilarItemRecTokens = extractAllRecTokens(actions);
monetateStore.pdp_similar_item = {
ids: pdpSimilarItemIds,
recTokens: pdpSimilarItemRecTokens,
};
return true;
} catch (error) {
console.error("Error handling productDetailViewEvent:", error);
return false;
}
}
Tracking Page Views
async trackPageView(url: string): Promise<ApiResponse | undefined> {
const event_object = {
eventType: "monetate:context:PageView",
url: url,
pageType: url,
};
return this.callEngineApi([event_object]);
}
Handling Impression Events
async handleImpression(): Promise<void> {
const event_object: MonetateEvent[] = [];
await this.callEngineApi(event_object);
}
Tracking Trolley Page View
async trackTrolleyPageView() {
const monetateStore = useMonetateStore();
const event_object: MonetateEvent[] = [
{
eventType: "monetate:context:PageView",
url: "https://www.toolstation.nl/",
pageType: "cart",
},
];
try {
const response = await this.callEngineApi(event_object);
const actions = this.filterActions(response, { actionType: "monetate:action:OmnichannelRecommendation" });
const trolley_actions: TrolleyActions = {
recently_viewed: getActionItems("recently-viewed", actions),
recommendations: getActionItems("recommendations", actions),
you_may_also_like: getActionItems("you-may-also-like", actions),
dont_miss_this: getActionItems("dont-miss-this", actions),
often_bought_together: getActionItems("Often-bought-together", actions),
frequently_purchased_together: getActionItems("Frequently-purchased-together", actions),
trolley_best_seller: getActionItems("trolley-best-seller", actions),
};
monetateStore.trolley_page_recommendations = trolley_actions;
return true;
} catch (e) {
let errorMeta = { event: event_object, monetateServiceError: true };
customClientErrorHandler(
e,
'high',
'Monetate Service Error - trying to track trolley page using trackTrolleyPageView()',
errorMeta
);
}
}
Conclusion
This document provides an overview of the Monetate Service API implementation.
To get list all of recommendations refer List
Knowledge base -