SDK Methods

the following methods are available to use in the monetate sdk all methods are part of the personalization class for parameters that require event names, refer to sdk event and action types for syntax generatemonetateid this method generates a monetate id and returns it as a string you can store this id locally for future use generatemonetateid method static generatemonetateid() string generatemonetateid method static generatemonetateid() string generatemonetateid method public func generatemonetateid () > string generatemonetateid method public static string generatemonetateid(); code example generatemonetateid method example const monetateid = personalization generatemonetateid() generatemonetateid method example const monetateid = personalization generatemonetateid() generatemonetateid method example let monetateid = self personalization generatemonetateid() generatemonetateid method example string monetateid = personalization generatemonetateid(); report reports an event to monetate this allows data to later be used for decisions within monetate use this method to report events report method report(context eventtypes, events contextdata) void report method report(context eventtypes, events contextdata) void report method report(context \<contextenum>, event \<mevent?>) report method public void report(string context, object events){} this method must be run in a thread the parameters are as follows parameter required description context yes the name of the event events yes the event data code example report method example const personalizationinstance = new personalization(user, account); const cart = { "cartlines" \[{ "sku" "sku1", "pid" "pid1", "quantity" 2, "currency" "usd", "value" "24 00" }] }; personalizationinstance report( eventtypes contextcart, { cartlines cart } ); report method example const personalizationinstance = new personalization(user, account); const cart = { "cartlines" \[{ "sku" "sku1", "pid" "pid1", "quantity" 2, "currency" "usd", "value" "24 00" }] }; personalizationinstance report( eventtypes contextcart, { cartlines cart } ); report method example objpersonalization report(context recclicks, event recclicks(recclicks \["rt 1 xxx", "rt 1 yyy"])) report method example account account = new account("localhost org", "a 701b337c", "p", "localhost"); user user = new user(); user setdeviceid("1109349827122"); try { personalization personalization = new personalization(user, account); ipaddress address = new ipaddress(); address setipaddress("127 0 0 0 "); new thread(new runnable() { @override public void run() { personalization report(eventtypes contextipaddress, address); } }) start(); } catch (accountdetailsexception e) { e printstacktrace(); } addevent this method adds event data to the sdk's internal stack use this method to add events to the stack that you intend to use to trigger experiences you can use multiple calls of this method to add multiple events to stack use this method with sdk methods docid\ f5siejxas3hi38daxdvpy when an experience requires data from more than one event addevent method addevent(context eventtypes, events contextdata) void addevent method addevent(context eventtypes, events contextdata) void addevent method addevent(context \<contextenum>, event \<mevent?>) addevent method public void addevent(string context, object event) the parameters are as follows parameter required description context yes the name of the event events yes the event data code example addevent method example personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); addevent method example personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); addevent method example objpersonalization addevent(context screensize, event screensize(height 1000, width 400)) objpersonalization addevent(context ipaddress, event ipaddress(ipaddress "192 168 1 52")) objpersonalization addevent(context pageview, event pageview(pagetype "pdp", path "n/a", url "n/a", categories \[], breadcrumbs \[])) addevent method example ipaddress ipaddress = new ipaddress(); ipaddress setipaddress("1 0 0 0"); screensize screensize = new screensize(); screensize setheight(1011); screensize setwidth(2011); pageview pageview = new pageview(); pageview\ setpagetype("cartpage"); pageview\ seturl("https //www monetate com"); personalization addevent(eventtypes contextipaddress, ipaddress); personalization addevent(eventtypes contextpageview, pageview); personalization addevent(eventtypes contextscreensize, screensize); getactionsdata requests an experience decision from monetate based off the action type you can specify multiple action types in an array to get multiple responses the experience decision depends on event data reported using sdk methods docid\ f5siejxas3hi38daxdvpy calls use addevent to add report all of the relevant events before you use this method to request a decision getactionsdata method getactionsdata(actiontype array\<string>, includereporting? boolean) promise\<any> the parameters are as follows parameter description actiontype the type of action you want to request you can specify one action or multiple actions in an array to handle includereporting indicates whether the response will have impression reporting data getactionsdata method getactionsdata(actiontype array\<string>, includereporting? boolean) promise\<any> the parameters are as follows actiontype is the type of action you want to request you can specify one action or multiple actions in an array to handle includereporting indicates whether the response will have impression reporting data getactionsdata method getactionsdata(requestid \<string>, includereporting \<bool>, arractiontypes <\[string]>) the parameters are as follows requestid is the request id for the api includereporting indicates whether the response will have impression reporting data arractiontypes is the type of actions you want to request you can specify multiple actions in an array to handle getactionsdata method public string getactionsdata(string\[] actiontypes, boolean includereports) the parameters are as follows actiontypes is the type of action you want to request you can specify one action or multiple actions in an array to handle includereports indicates whether the response will have impression reporting data code examples the following example specifies a single action getactionsdata method example personalizationinstance getactionsdata("monetate\ action\ omnichanneljson") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata method example personalizationinstance getactionsdata("monetate\ action\ omnichanneljson") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata method example objpersonalization getactionsdata( requestid "123456", includereporting false, arractiontypes \["monetate\ action\ omnichannelrecommendation"] ) on { res in if res status == 200 { self handlerecommendations(res res) } else { } } getactionsdata method example try { personalization personalization = new personalization(user, account); // events and context data ipaddress ipaddress = new ipaddress(); ipaddress setipaddress("1 0 0 0"); screensize screensize = new screensize(); screensize setheight(1011); screensize setwidth(2011); pageview pageview = new pageview(); pageview\ setpagetype("cartpage"); pageview\ seturl("https //www monetate com"); // addevent personalization addevent(eventtypes contextscreensize, screensize); personalization addevent(eventtypes contextipaddress, ipaddress); personalization addevent(eventtypes contextpageview, pageview); // getactionsdata string responsedata; new thread(new runnable() { @override public void run() { // gets the responsedata using getactionsdata responsedata = personalization getactionsdata("monetate\ action\ omnichanneljson", true); // once you receive the responsedata from getactionsdata, use the handler and parse the required data from it new handler(looper getmainlooper()) post(new runnable() { @override public void run() { // parse the received responsedata, get the requireddata from it and use it accordingly try { // example textview\ settext(requireddata); } catch (exception ex) { // catch any exception that occurs while parsing ex printstacktrace(); } } }); } }) start(); } catch (accountdetailsexception e) { e printstacktrace(); } the following example specifies multiple actions getactionsdata method example with multiple actions // for multiple actions, send an array // when an empty array in passed as an argument, it will fetch all the configured omnichannel actions const requiredactions \[ "monetate\ action\ omnichannelrecommendation" , "monetate\ action\ socialproofdatav2" ] personalizationinstance getactionsdata(requiredactions) then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata method example with multiple actions // for multiple actions, send an array // when an empty array in passed as an argument, it will fetch all the configured omnichannel actions const requiredactions \[ "monetate\ action\ omnichannelrecommendation" , "monetate\ action\ socialproofdatav2" ] personalizationinstance getactionsdata(requiredactions) then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata method example with multiple actions objpersonalization getactionsdata( requestid "123456", includereporting false, arractiontypes \["monetate\ action\ omnichanneljson", "monetate\ action\ omnichannelrecommendation"] ) on { res in if res status == 200 { self handlerecommendations(res res) } else { } } getactionsdata method example with multiple actions try { personalization personalization = new personalization(user, account); // events and context data ipaddress ipaddress = new ipaddress(); ipaddress setipaddress("1 0 0 0"); screensize screensize = new screensize(); screensize setheight(1011); screensize setwidth(2011); pageview pageview = new pageview(); pageview\ setpagetype("cartpage"); pageview\ seturl("https //www monetate com"); // addevent personalization addevent(eventtypes contextscreensize, screensize); personalization addevent(eventtypes contextipaddress, ipaddress); personalization addevent(eventtypes contextpageview, pageview); // getactionsdata string responsedata; new thread(new runnable() { @override public void run() { // gets the responsedata using getactionsdata responsedata = personalization getactionsdata(new string\[]{"monetate\ action\ omnichanneljson","monetate\ action\ omnichannelrecommendation"}, true); // once you receive the responsedata from getactionsdata, use the handler and parse the required data from it new handler(looper getmainlooper()) post(new runnable() { @override public void run() { // parse the received responsedata, get the requireddata from it and use it accordingly try { // example textview\ settext(requireddata); } catch (exception ex) { // catch any exception that occurs while parsing ex printstacktrace(); } } }); } }) start(); } catch (accountdetailsexception e) { e printstacktrace(); } getactions reports an event and immediately requests a decision from monetate use this method if an experience you want to trigger requires a single event this method returns a json object that includes the response data the response data can then be used in your application for example, you can use this method to obtain data to display as a banner on a page getactions method getactions(actiontype, context?, eventdata?, includereporting?) promise\<any> the parameters are as follows parameter description actiontype the type of action you want to request you can specify multiple actions in an array to handle context the name of the event eventdata the data associated with the event includereporting indicates whether the response will have impression reporting data getactions method getactions(actiontype, context?, eventdata?, includereporting?) promise\<any> parameters actiontype is the type of action you want to request you can specify multiple actions in an array to handle context is name of the event eventdata is the data associated with the event includereporting indicates whether the response will have impression reporting data getactions method objpersonalization getactions(context \<contextenum>, requestid \<string>, includereporting \<bool>, arractiontypes <\[string]>, event \<mevent?>) parameters context is name of the event requestid is the request id for the api includereporting indicates whether the response will have impression reporting data arractiontypes is the type of actions you want to request you can specify multiple actions in an array to handle event is the data associated with the event getactions method public string getactions(string context, object event, string\[] actiontypes, boolean includereports) {returns response;} the parameters are as follows actiontypes is the type of action you want to request you can specify one action or multiple actions in an array to handle context is name of the event event is the data associated with the event includereports indicates whether the response will have impression reporting data code example getactions method example import react, { useeffect, usestate} from "react" ; import { view , text} from "react native" ; import personalization from '@personalization js sdk/react native'; import { actiontypes, eventtypes } from "@personalization js sdk/common"; const test = () => { const personalizationinstance = new personalization(account, user) const \[actiondata, setactiondata] = usestate({}); const handlegetactions = () => { const eventtype = eventtypes contextcart; personalizationinstance getactions("monetate\ action\ omnichanneljson", eventtype) then((res) => { if (res length > 0) { res\[0] actions map((action) => { if (action json meta && action json meta tool === 'react native') { const data = { color action json data json color, text action json data json text, fontsize action json data json fontsize, style action json data json style, buttontext action json data json buttontext, buttoncolor action json data json buttoncolor, }; setactiondata(prevstate => { return { prevstate, decisioneventdata data, } }); } else { console warn('meta object not present in omni json '); } }); } }) catch((error) => console warn('please try after some time!', error)); }; useeffect(() => { handlegetactions(); }, \[]); return \<view> \<text> {actiondata buttontext} //render data on ui \</view>; }; getactions method example import react, { useeffect, usestate} from "react" ; import personalization from '@personalization js sdk/web'; import { actiontypes, eventtypes } from "@personalization js sdk/common"; const test = () => { const personalizationinstance = new personalization(account, user) const \[actiondata, setactiondata] = usestate({}); const handlegetactions = () => { const eventtype = eventtypes contextcart; personalizationinstance getactions("monetate\ action\ omnichanneljson", eventtype) then((res) => { if (res length > 0) { res\[0] actions map((action) => { if (action json meta && action json meta tool === 'web') { const data = { color action json data json color, text action json data json text, fontsize action json data json fontsize, style action json data json style, buttontext action json data json buttontext, buttoncolor action json data json buttoncolor, }; setactiondata(prevstate => { return { prevstate, decisioneventdata data, } }); } else { console warn('meta object not present in omni json '); } }); } }) catch((error) => console warn('please try after some time!', error)); }; useeffect(() => { handlegetactions(); }, \[]); return \<div> \<p> {actiondata buttontext} //render data on ui \</p> \</div>; }; getactions method example import marqueelabel import uikit import monetate ios sdk class categoryviewcontroller uiviewcontroller { @iboutlet weak var bottomlabel marqueelabel! @iboutlet weak var bottomview uiview! @iboutlet weak var constraintheightbottomview nslayoutconstraint! final var objpersonalization = personalization( account account( instance "p", domain "localhost org", name "a 701b337c", shortname "localhost"), user user(deviceid "62bd2e2d 213d 463f 83bb 12c0b2530a14")) override func viewdidload() { super viewdidload() self bottomview\ ishidden = true self constraintheightbottomview\ constant = 0 bottomlabel text = "" } override func viewwillappear( animated bool) { super viewwillappear(animated) pageevent() } func pageevent() { objpersonalization getactions( context pageview, requestid "test request id", includereporting false, arractiontypes \["monetate\ action\ omnichanneljson"], event pageview( pagetype "homepage", path "n/a", url "n/a", categories \[], breadcrumbs \[]) ) on { (res) in if res status == 200 { let data = json(res data) print(data) self handleaction(res res) } else { } } } fileprivate func handleaction(res apiresponse) { let data = json(res data) for item in data\["data"]\["responses"] arrayvalue { bottomlabel animationcurve = linear bottomlabel speed = duration(5) bottomlabel fadelength = 15 0 if item\["requestid"] string == res requestid { for oneaction in item\["actions"] arrayvalue { let component = oneaction\["component"] string ?? "" if component lowercased() == "footer" { if let json = oneaction\["json"] dictionary { print("final dict \\(json)") if let text = json\["text"]? string { self bottomview\ ishidden = false self constraintheightbottomview\ constant = 60 bottomlabel text = text if let fontstyle = json\["style"]? string, let fontsize = json\["fontsize"]? double { if fontstyle == "bold" { bottomlabel font = uifont(name "roboto bold", size fontsize) } else if fontstyle == "normal" { bottomlabel font = uifont(name "roboto regular", size fontsize) } else if fontstyle == "italic" { bottomlabel font = uifont(name "roboto italic", size fontsize) } } if let color = json\["color"]? string { bottomlabel textcolor = miscclass hexstringtouicolor(hex color) } if let backgroundcolor = json\["background color"]? string { bottomview\ backgroundcolor = miscclass hexstringtouicolor(hex backgroundcolor) return } } } } } } } } } getactions method example account account = new account("localhost org", "a 701b337c", "p", "localhost"); user user = new user(); user setdeviceid("1109349827122"); try { personalization personalization = new personalization(user, account); ipaddress address = new ipaddress(); address setipaddress("127 0 0 0 "); string responsedata; new thread(new runnable() { @override public void run() { // get the responsedata using getactions responsedata = personalization getactions(eventtypes contextipaddress, address, new string\[] {"monetate\ action\ omnichanneljson"}, true); // once you receive the responsedata from getactions, use the handler and parse the required data from it new handler(looper getmainlooper()) post(new runnable() { @override public void run() { // parse the received responsedata, get the requireddata from it and use it accordingly try { // example textview\ settext(requireddata); } catch (exception ex) { // catch any exception that occurs while parsing ex printstacktrace(); } } }); } }) start(); } catch (accountdetailsexception e) { e printstacktrace(); } flush immediately sends all event data that are currently queued use this method if you want to report an event immediately this method returns a response of success or failure this method might throw the following exceptions that you must handle interruptedexception executionexception timeoutexception flush method flush() flush method flush() flush method flush() flush method public string flush() throws interruptedexception, executionexception, timeoutexception {} code example flush method example const personalization= new personalization(account, user); personalizationinstance report ( 'monetate\ context\ ipaddress', { ipaddress 10 10 10 10 } ), personalization flush(); you can also capture the method response in a variable flush method reponse example const response = personalization flush() flush method example const personalization= new personalization(account, user); personalizationinstance report ( 'monetate\ context\ ipaddress', { ipaddress 10 10 10 10 } ), personalization flush(); you can also capture the method response in a variable flush method response example const response = personalization flush() flush method example objpersonalization report(context recclicks, event recclicks(recclicks \["rt 1 xxx", "rt 1 yyy"])) objpersonalization report(context recimpressions, event recimpressions(recimpressions \["rt 2 xxx", "rt 2 yyy"])) objpersonalization flush() flush method example try { personalization personalization = new personalization(user, account); ipaddress address = new ipaddress(); address setipaddress("127 0 0 0 "); new thread(new runnable() { @override public void run() { personalization report(eventtypes contextipaddress, address); personalization flush(); } }) start(); } catch (exception e) { e printstacktrace(); } setcustomerid updates the customerid within the sdk objects setcustomerid method setcustomerid(customerid) setcustomerid method setcustomerid(customerid) setcustomerid method setcustomerid(customerid) setcustomerid method public void setcustomerid(string customerid); the parameter is as follows customerid is a string containing the customer id (required) code example setcustomerid method example personalizationinstance setcustomerid("test customer id"); setcustomerid method example personalizationinstance setcustomerid("test customer id"); setcustomerid method example objpersonalization setcustomerid(customerid "155468") setcustomerid method example personalization setcustomerid("test customer id");