Context Layer Implementation Guide
context methods reference addallcartdata signature android public void addallcartdata(cartline\[] cartlines) ios public func addallcartdata( cartlines \[cartline]?) throws 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 https //developer monetate com/sdks/context events parameters#rhcwp parameters usage example android cartline cartline1 = new cartline(); cartline1 setsku("sku 1"); cartline1 setpid("pid 1"); cartline1 setquantity(2); cartline1 setcurrency("us"); cartline1 setvalue("20"); cartline cartline2 = new cartline(); cartline2 setsku("sku 2"); cartline2 setpid("pid 2"); cartline2 setquantity(1); cartline2 setcurrency("us"); cartline2 setvalue("10"); context context = new context(); context addallcartdata(new cartline\[] { cartline1, cartline2 }); ios let contextobj = contextobj() let cartlinessample = \[ cartline(sku "sku123", pid "pid123", quantity 1, currency "usd", value "19 99"), cartline(sku "sku456", pid "pid456", quantity 2, currency "usd", value "39 98") ] do { try contextobj addallcartdata(cartlinessample) } catch { print(error localizeddescription) } signature android public void addallcartdata(cartline\[] cartlines) ios public func addallcartdata( cartlines \[cartline]?) throws 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 https //developer monetate com/sdks/context events parameters#rhcwp parameters usage example android cartline cartline1 = new cartline(); cartline1 setsku("sku 1"); cartline1 setpid("pid 1"); cartline1 setquantity(2); cartline1 setcurrency("us"); cartline1 setvalue("20"); cartline cartline2 = new cartline(); cartline2 setsku("sku 2"); cartline2 setpid("pid 2"); cartline2 setquantity(1); cartline2 setcurrency("us"); cartline2 setvalue("10"); context context = new context(); context addallcartdata(new cartline\[] { cartline1, cartline2 }); ios let contextobj = contextobj() let cartlinessample = \[ cartline(sku "sku123", pid "pid123", quantity 1, currency "usd", value "19 99"), cartline(sku "sku456", pid "pid456", quantity 2, currency "usd", value "39 98") ] do { try contextobj addallcartdata(cartlinessample) } catch { print(error localizeddescription) } addsinglecartdata method signature android public void addsinglecartdata(cartline cartline) description sets a single cart item on the context object use during add to cart operations refer to the https //developer monetate com/sdks/context events parameters#rhcwp parameters usage example android cartline cartline = new cartline(); cartline setsku("sku"); cartline setpid("pid"); cartline setquantity(2); cartline setcurrency("us"); cartline setvalue("20"); context context = new context(); context addsinglecartdata(cartline); method signature ios public func addsinglecartdata( cartline cartline?) throws description sets a single cart item on the context object use during add to cart operations refer to the https //developer monetate com/sdks/context events parameters#rhcwp parameters usage example ios let contextobj = contextobj() let cartline = cartline(sku "sku6", pid "pid4", quantity 3, currency "curr1", value "21") do { try contextobj addsinglecartdata(cartline) } catch { print(error localizeddescription) } notes all required fields must be populated in the cartline object addcustomvariablesdata method signature android public void addcustomvariablesdata(customvariable\[] customvariables) description sets one or more custom variables for business specific attributes refer to the https //developer monetate com/sdks/context events parameters#fykgn parameters usage example android 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 } ); method signature ios public func addcustomvariablesdata( customvariables \[customvariablesmodel]?) throws description sets one or more custom variables for business specific attributes refer to the https //developer monetate com/sdks/context events parameters#fykgn parameters usage example ios let contextobj = contextobj() let customvariablesmodels = \[ customvariablesmodel(variable "usertype", value "premium"), customvariablesmodel(variable "loyaltylevel", value 3) ] try? contextobj addcustomvariablesdata(customvariablesmodels) addipaddress method signature android public void addipaddress(string ipaddress)public func addipaddress( ipaddress string?) throws description sets the ip address on the context object refer to the https //developer monetate com/sdks/context events parameters#xbdip parameters usage example android context context = new context(); context addipaddress("1 0 0 0"); method signature ios public func addipaddress( ipaddress string?) throws description sets the ip address on the context object refer to the https //developer monetate com/sdks/context events parameters#xbdip parameters usage example ios let contextobj = contextobj() try? contextobj addipaddress("192 168 1 1") addmetadata method signature android public void addmetadata(language language) description sets metadata on the context object currently used for language context refer to the https //developer monetate com/sdks/context events parameters#4soea parameters usage example android language language = new language(); language setlanguage("en"); context context = new context(); context addmetadata(language); method signature ios public func addmetadata(language language?) throws description sets metadata on the context object currently used for language context refer to the https //developer monetate com/sdks/context events parameters#4soea parameters usage example ios let contextobj = contextobj() let language = language(language "en us") try? contextobj addmetadata(language language) addpagedetails method signature android public void addpagedetails( string pagetype, string url, string\[] categories, string\[] breadcrumbs, string path ) description sets page level details including pagetype , url/path , categories , and breadcrumbs refer to the https //developer monetate com/sdks/context events parameters#ie5eb parameters usage example android context context = new context(); context addpagedetails( "home page", "https //example com", new string\[] { "category1", "category2" }, new string\[] { "breadcrumb1", "breadcrumb2" }, "abc/xyz/pqr/testpath" ); method signature ios let contextobj = contextobj() public func addpagedetails(pagetype string, path string?, url string?, categories \[string]?, breadcrumbs \[string]?) throws description sets page level details including pagetype , url/path , categories , and breadcrumbs refer to the https //developer monetate com/sdks/context events parameters#ie5eb parameters usage example ios let contextobj = contextobj() try? contextobj addpagedetails(pagetype "home", path "path", url nil, categories \["electronics", "sale"], breadcrumbs \["home", "deals"] ) notes pagetype is mandatory either url or path is required addpageeventsdata method signature android public void addpageeventsdata(string\[] pageeventsdata) description sets page event identifiers on the context object refer to the https //developer monetate com/sdks/context events parameters#4r yd parameters usage example android context context = new context(); context addpageeventsdata(new string\[] { "eventdata1", "eventdata2" }); method signature ios public func addpageeventsdata( pageeventsdata \[string]?) throws description sets page event identifiers on the context object refer to the https //developer monetate com/sdks/context events parameters#4r yd parameters usage example ios let contextobj = contextobj() try? contextobj addpageeventsdata(\["pageload", "buttonclick", "formsubmit"]) addproductdetails method signature android public void addproductdetails(product\[] productsdata) description sets product level details such as productid and sku refer to the https //developer monetate com/sdks/context events parameters#r1pba parameters usage example android product product1 = new product(); product1 setproductid("pid1"); product1 setsku("sku1"); product product2 = new product(); product2 setproductid("pid2"); product2 setsku("sku2"); context context = new context(); context addproductdetails(new product\[] { product1, product2 }); method signature ios public func addproductdetails( productsdata \[product]?) throws description sets product level details such as productid and sku refer to the https //developer monetate com/sdks/context events parameters#r1pba parameters usage example ios let productssample = \[ product(productid "prod001", sku "sku001"), product(productid "prod002", sku "sku002") ] let contextobj = contextobj() try? contextobj addproductdetails(productssample) addproductthumbnailsdata method signature android public void addproductthumbnailsdata(string\[] products) ios public func addproductthumbnailsdata( products \[string]?) throws description sets product thumbnail view data refer to the https //developer monetate com/sdks/context events parameters#siirq parameters usage example android context context = new context(); context addproductthumbnailsdata(new string\[] { "product21", "product43" }); ios let contextobj = contextobj() try? contextobj addproductthumbnailsdata(\["prod001", "prod002", "prod003"]) method signature ios public func addproductthumbnailsdata( products \[string]?) throws description sets product thumbnail view data refer to the https //developer monetate com/sdks/context events parameters#siirq parameters usage example ios let contextobj = contextobj() try? contextobj addproductthumbnailsdata(\["prod001", "prod002", "prod003"]) addpurchasedata method signature android public void addpurchasedata(string purchaseid, purchaseline\[] purchaselinedata) description sets purchase transaction data along with purchased line items refer to the https //developer monetate com/sdks/context events parameters#f37yx and https //developer monetate com/sdks/context events parameters#5pklq parameters usage example android 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 }); method signature ios public func addpurchasedata(purchaseid string?, purchaselinedata \[purchaseline]?) throws description sets purchase transaction data along with purchased line items refer to the https //developer monetate com/sdks/context events parameters#f37yx and https //developer monetate com/sdks/context events parameters#5pklq parameters usage example ios let purchaselinessample \[purchaseline] = \[ purchaseline(sku "sku0", pid "pid123", quantity 1, currency "usd", value "19 99"), purchaseline(sku "sku1", pid "pid456", quantity 2, currency "usd", value "39 98") ] let contextobj = contextobj() try? contextobj addpurchasedata(purchaseid "purchase123", purchaselinedata purchaselinessample) addscreensizedata method signature android public void addscreensizedata(integer screenheight, integer screenwidth) description sets screen size information usage example android context context = new context(); context addscreensizedata(211, 2313); method signature ios public func addscreensizedata(screenheight int?, screenwidth int?) description sets screen size information refer to the https //app archbee com/docs/gi3o1i4xddy584dqsyxqi/n3hsu6yazaw65bljfetrg#0aeht parameters usage example ios let contextobj = contextobj() try? contextobj addscreensizedata(screenheight 1000, screenwidth 400) addcoordinatesdata method signature android public void addcoordinatesdata(string latitude, string longitude) description sets coordinate (latitude, longitude) data refer to the https //app archbee com/docs/gi3o1i4xddy584dqsyxqi/n3hsu6yazaw65bljfetrg#emwlu parameters usage example android context context = new context(); context addcoordinatesdata("19 0760", "72 8777"); method signature ios public func addcoordinatesdata(latitude string?, longitude string?) description sets coordinate (latitude, longitude) data usage example ios let contextobj = contextobj() try? contextobj addcoordinatesdata(latitude "37 7749", longitude " 122 4194") adduseragentdata method signature android public void adduseragentdata(string data) description sets user agent / device information refer to the https //app archbee com/docs/gi3o1i4xddy584dqsyxqi/n3hsu6yazaw65bljfetrg#6o rn parameters usage example android devicedetails details = new devicedetails(); details setdevice("device name here"); details setmodel("device model here"); details setosapilevel("os api level here"); details setosversion("os version here"); details setproductname("product name here"); string devicedetails = personalization refactordevicedetails(details); context context = new context(); context adduseragentdata(devicedetails); method signature android public void adduseragentdata(string data) ios public func adduseragentdata( data string?) throws description sets user agent / device information usage example android devicedetails details = new devicedetails(); details setdevice("device name here"); details setmodel("device model here"); details setosapilevel("os api level here"); details setosversion("os version here"); details setproductname("product name here"); // string devicedetails = personalization refactordevicedetails(details); string devicedetails = personalization refactordevicedetails(null); context context = new context(); context adduseragentdata(devicedetails); ios try? contextobj adduseragentdata("mozilla/5 0 (iphone; cpu iphone os 14 0 like mac os x) applewebkit/605 1 15 (khtml, like gecko) version/14 0 mobile/15e148 safari/604 1") 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 android try { context addipaddress("1 0 0 0"); } catch (illegalargumentexception e) { // handle validation error } all context methods validate required inputs before setting data if any required field is missing null empty the sdk throws an illegalargumentexception ios do { try contextobj addipaddress("1 0 0 0") } catch { // handle validation error } context layer for event data reporting the context layer allows you to pass event specific data to the sdk’s https //developer monetate com/sdks/methods#59alm reporting cart data example android // seeting up user details user user = new user(); user setdeviceid("my deviceid"); //setting up account details account account = new account("monetate mybigcommerce com", "a 3e41bf76", "p", "monetatebc"); //creating personalization instance personalization personalization = new personalization(user, account); //settingcartline data cartline cartline1 = new cartline(); cartline1 setsku("sku1"); cartline1 setpid("pid1"); cartline1 setquantity(2); cartline1 setcurrency("us"); cartline1 setvalue("20"); cartline cartline2 = new cartline(); cartline2 setsku("sku2"); cartline2 setpid("pid2"); cartline2 setquantity(1); cartline2 setcurrency("us"); cartline2 setvalue("10"); //setting data to context context context = new context(); context addallcartdata(new cartline\[] { cartline1, cartline2 }); //reporting entire cartdata personalization reportcartdata(context); ios let cartlinessample = \[ cartline(sku "sku123", pid "pid123", quantity 1, currency "usd", value "19 99"), cartline(sku "sku456", pid "pid456", quantity 2, currency "usd", value "39 98") ] let contextobj = contextobj() try? contextobj addallcartdata(cartlinessample) personalization reportcartdata(contextdata contextobj) using context with getactions() the https //developer monetate com/sdks/methods#djr q 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 android // seeting up user details user user = new user(); user setdeviceid("my deviceid"); //setting up account details account account = new account("monetate mybigcommerce com", "a 3e41bf76", "p", "monetatebc"); //creating personalization instance personalization personalization = new personalization(user, account); //setting data to context context context = new context(); context addipaddress("1 0 0 0"); //calling getactions personalization getactions( context, new string\[] { "monetate\ action\ omnichanneljson" }, false, new actionscallback() { @override public void onsuccess(string actions) { system out println("actions data " + actions); } @override public void onerror(exception e) { system out println("error " + e getmessage()); } } ); ios let contextobj = contextobj() try? contextobj addipaddress("1 0 0 0") objpersonalization getactions(context contextobj, arractiontypes \["monetate\ action\ omnichanneljson"], includereporting false) on(success { (actions) in self handleaction(actions actions) }) { err in print(err localizeddescription) } example 2 multi context evaluation android context context = new context(); // setting multipole data to context context addipaddress("1 0 0 0"); context addpagedetails( "home page", "https //example com", new string\[] { "category1" }, new string\[] { "breadcrumb1" }, "abc/xyz" ); // calling getactions personalization getactions( context, new string\[] { "monetate\ action\ omnichanneljson" }, false, new actionscallback() { @override public void onsuccess(string actions) { system out println("actions data " + actions); } @override public void onerror(exception e) { system out println("error " + e getmessage()); } } ); ios let contextobj = contextobj() try? contextobj addipaddress("1 0 0 0") try? contextobj addpagedetails(pagetype "home", path "deals/offers", url nil, categories \["electronics", "sale"], breadcrumbs \["home", "deals"]) objpersonalization getactions(context contextobj, arractiontypes \["monetate\ action\ omnichanneljson"], includereporting false) on(success { (actions) in self handleaction(actions actions) }) { err in print(err localizeddescription) } the sdk evaluates all data present inside the context object and returns the appropriate actions