Context Layer Implementation Guide
Context Methods Reference
- addAllCartData
Method Signature
addAllCartData(): {
cartLines(products: cartLine[]): this;
}Description
- Sets the complete cart information on the Context object.
- Use this method when you want to provide the entire cart state in a single operation.
- Refer to the Cart Lines parameters.
Usage Example
let context = new Context();
monetateContext.addAllCartData().product({
sku: "SKU-123",
pid: "ddsds",
productId: "123",
quantity: 1,
value: "1999",
currency: "INR",
});
- addSingleCartData
Method Signature
addSingleCartData(): {
product(product: ProductLine): this;
}Description
- Sets a single cart item on the Context object.
- Use during Add To Cart operations.
- Refer to the Cart Lines parameters.
Usage Example
let context = new Context();
context
.addSingleCartData()
.product({
sku: "sku-1",
pid: "parent-1",
productId: "prod-1",
quantity: 1,
value: "50",
currency: "USD"
});- addCustomVariablesData
Method Signature
public void addCustomVariablesData(CustomVariable[] customVariables)Description
- Sets one or more custom variables for business-specific attributes.
- Refer to the Custom variables parameters.
Usage Example
CustomVariable customVariable1 = new CustomVariable();
customVariable1.setVariable("Team A");
customVariable1.setValue("Winning Team");
CustomVariable customVariable2 = new CustomVariable();
customVariable2.setVariable("Team B");
customVariable2.setValue("Losing Team");
Context context = new Context();
context.addCustomVariablesData(
new CustomVariable[] { customVariable1, customVariable2 }
);- addIpAddress
Method Signature
public void addIpAddress(String ipAddress)public func addIpAddress(_ ipAddress: String?) throws
Description
- Sets the IP address on the Context object.
- Refer to the IPAddress parameters.
Usage Example
Context context = new Context();
context.addIpAddress("1.0.0.0");- addMetaData
Method Signature
public func addMetaData(language: Language?) throwsDescription:-
- Sets metadata on the Context object.
- Currently used for language context.
- Refer to the Meta Data event parameters.
Usage Example
let contextObj = ContextObj()
let language = Language(language: "en-US")
try? contextObj.addMetaData(language: language)- addPageDetails
Method Signature :-
addPageDetails(): {
url(url: string): this;
path(path: string): this;
pageType(pageType: string): this;
breadcrumbs(breadcrumbs: string[]): this;
categories(categories: string[]): this;
}Description
- Sets page-level details including pageType, URL/path, categories, and breadcrumbs.
- Refer to the Page Detail View event parameters.
Usage Example
Context context = new Context();
context
.addPageDetails()
.url(window.location.href)
.path(window.location.pathname)
.pageType("home")
.breadcrumbs(["Home"])
.categories(["Homepage"]);
- addPageEventsData
Method Signature
public void addPageEventsData(String[] pageEventsData)Description
- Sets page event identifiers on the Context object.
- Refer to the Page Events parameters.
Usage Example
Context context = new Context();
context.addPageEventsData(new String[] { "eventData1", "eventData2" });- addProductDetails
Method Signature
addProductDetails(): {
product(product: { sku: string; productId?: string }): this;
}Description
- Sets product-level details such as productID and SKU.
- Refer to the Product Detail View Event parameters.
Usage Example
const context = new Context();
context
.addProductDetails()
.productId("123")
.productSKU("SKU-1");- addProductThumbnailsData
Method Signature
public func addProductThumbnailsData(_ products: [String]?) throwsDescription
- Sets product thumbnail view data.
- Refer to the Product Thumbnails Event parameters.
Usage Example
let contextObj = ContextObj()
try? contextObj.addProductThumbnailsData(["PROD001", "PROD002", "PROD003"])- addPurchaseData
Method Signature
public void addPurchaseData(String purchaseId, PurchaseLine[] purchaseLineData)Description
- Sets purchase transaction data along with purchased line items.
- Refer to the Purchase Event and Purachse Lines parameters.
Usage Example
PurchaseLine purchaseLine = new PurchaseLine();
purchaseLine.setSku("SKU1");
purchaseLine.setPid("PID1");
purchaseLine.setQuantity(2);
purchaseLine.setCurrency("US");
purchaseLine.setValue("23.00");
Context context = new Context();
context.addPurchaseData("1b2c3d", new PurchaseLine[] { purchaseLine });
- addScreenSizeData
Method Signature
addScreenSizeData(): {
width(width: number): this;
height(height: number): this;
}Description
- Sets screen size information.
- Refer to the ScreenSize Event parameters.
Usage Example
context
.addScreenSizeData()
.width(211)
.height(400);- addCoordinatesData
Method Signature
addCoordinatesData(): {
latitude(latitude: string): this;
longitude(longitude: string): this;
}Description
- Sets coordinate (latitude, longitude) data.
Usage Example
Context context = new Context();
context
.addCoordinatesData()
.latitude("12.9716")
.longitude("77.5946");
- addUserAgentData
Method Signature
addUserAgentData(): {
userAgent(userAgent: string): this;
}Description
- Sets user agent / device information.
- Refer to the UserAgent Event parameters.
Usage Example
Context context = new Context();
context
.addUserAgentData()
.userAgent(navigator.userAgent);
Validation & Error Handling
All Context methods validate required inputs before setting data.
If any required field is:
- Missing
- Null
- Empty
the SDK throws an IllegalArgumentException.
try {
context.addIpAddress("1.0.0.0");
} catch (err) {
// Handle validation error
}Context Layer for Event Data Reporting
The Context Layer allows you to pass event-specific data to the SDK’s reporting methods.
Reporting Cart data
example :-
const account = {
account: "monetate",
domain: "monetate",
name: "a-123445",
instance: "p",
shortname: "monetate",
engineHostName:"host-name"
};
const user = {
deviceId: "",
monetateId: Personalization.generateMonetateId(),
customerId: "customerId",
};
const personalization = new Personalization(user, account);
let context = new Context();
monetateContext.addAllCartData().product({
sku: "SKU-123",
pid: "ddsds",
productId: "123",
quantity: 1,
value: "1999",
currency: "INR",
});
//reporting entire CartData
personalization.reportCartData();Using Context with getActions()
The getActions method retrieves experience-based actions from the personalization engine.
The Context Layer provides all input signals required by the experience rules (IP, page details, product details, etc.). In Single-Context evaluation only single data is required to satisfy the experience In Multi-Context evaluation multiple data is required to satisy the experience
Example 1: Single-Context Evaluation
const account = {
account: "monetate",
domain: "monetate",
name: "a-123445,
instance: "p",
shortname: "monetate",
engineHostName:"host-name"
};
const user = {
deviceId: "",
monetateId: Personalization.generateMonetateId(),
customerId: "customerId",
};
const personalization = new Personalization(user, account);
//setting data to context
const monetateContext = new Context();
context.addIpAddress("1.0.0.0");
coreSdkInstance.getActions(
["monetate:action:OmnichannelJson"] , monetateContext)
.then((res: any) => console.log("Action ", res));Example 2: Multi-Context Evaluation
const account = {
account: "monetate",
domain: "monetate",
name: "a-123445,
instance: "p",
shortname: "monetate",
engineHostName:"host-name"
};
const user = {
deviceId: "",
monetateId: Personalization.generateMonetateId(),
customerId: "customerId",
};
const personalization = new Personalization(user, account);
//setting data to context
const monetateContext = new Context();
context.addIpAddress("1.0.0.0");
monetateContext.addAllCartData().product({
sku: "SKU-123",
pid: "ddsds",
productId: "123",
quantity: 1,
value: "1999",
currency: "INR",
});
coreSdkInstance.getActions(
["monetate:action:OmnichannelJson" ], monetateContext)
.then((res: any) => console.log("Action ", res));The SDK evaluates all data present inside the Context object and returns the appropriate actions.