Actions
Create a Recommendations Action
omnichannel recommendations is an integrated product recommendations experience that is consistent across all platforms that you connect to monetate whether a customer is viewing your storefront through a mobile app or your site, omnichannel recommendations ensure a consistent experience you can set up a handler for an omnichannel recommendations action by using two methods the addevents method defines the events that can trigger the action the getactionsdata method is then used as the trigger and requests the decision based on the defined events getactionsdata then returns a json object containing recommendations data that you can then handle in code prerequisites you must first create an omnichannel experience in the monetate platform for the methods to reference refer to configure an omnichannel recommendations action in the monetate knowledge base for instructions make note of the monetate core components because they correspond to events your code listens for the example experience in this article uses the following who settings ip address is 1 0 0 2 screen height is at least 500 pixels and screen width is at least 300 pixels the example code in this article fulfills these conditions and will trigger the omnichannel experience addevent this method adds event data to the sdk's internal stack addevent method addevent(context eventtypes, events contextdata) void the parameters are as follows context is name of the event events is the event data addevent method addevent(context eventtypes, events contextdata) void the parameters are as follows context is name of the event events is the event data addevent method addevent(context \<contextenum>, event \<mevent?>) the parameters are as follows context is name of the event (required) event is the event data (required) addevent method public void addevent(string context, object event) the parameters are as follows context is name of the event (required) event is the event data (required) you can use this method multiple times to add all the necessary events for an experience you might want to trigger the example code uses multiple method calls to fulfill the experience requirements code example addevent example personalizationinstance addevent(eventtypes contextscreensize, { width 23, height 34 }); personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); addevent example personalizationinstance addevent(eventtypes contextscreensize, { width 23, height 34 }); personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); addevent 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 example personalization addevent(eventtypes contextipaddress, new ipaddress("1 0 0 0")); personalization addevent(eventtypes contextpageview, new pageview("home", "test com",new string\[] {"categoies"} ,new string\[] {"breadcrums"})); personalization addevent(eventtypes contextscreensize, new screensize(1011, 2011)); getactionsdata this method sends the defined events to monetate to trigger an experience if the events fulfill the who settings of an experience, then that experience is triggered a json object containing the experience response is then returned getactionsdata method getactionsdata(actiontype array\<string>, includereporting? boolean) promise\<any> the parameter is 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(actiontype array\<string>, includereporting? boolean) promise\<any> the parameter is 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 action you want to request you can specify one action or 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 example getactionsdata example let recdata; personalizationinstance getactionsdata("monetate\ action\ omnichannelrecommendation") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata example let recdata; personalizationinstance getactionsdata("monetate\ action\ omnichannelrecommendation") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); getactionsdata example objpersonalization getactionsdata(requestid "123456", includereporting false, arractiontypes \["monetate\ action\ omnichannelrecommendation"]) on{res in if res status==200 { self handlerecommendations(res res) } else { } } } getactionsdata example string responsedata = personalization getactionsdata(new string\[]{"monetate\ action\ omnichannelrecommendation"},true); full code example recommendations action full example // add context / events // personalizationinstance addevent(eventtypes contextscreensize, { width 23, height 34 }); personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); // // get actions let recdata; personalizationinstance getactionsdata("monetate\ action\ omnichannelrecommendation") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); recommendations action full example // add context / events // personalizationinstance addevent(eventtypes contextscreensize, { width 23, height 34 }); personalizationinstance addevent(eventtypes contextipaddress, { ipaddress "10 0 0 2", }); personalizationinstance addevent(eventtypes contextpageview, { url "http //www monetate com/index html", pagetype "home" }); // get actions // let recdata; personalizationinstance getactionsdata("monetate\ action\ omnichannelrecommendation") then(res => { recsdata = res\[0] actions; }) catch(error => { console warn('error!', error); }); recommendations action full example // add context / events // 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 \[])) // get actions // objpersonalization getactionsdata( requestid "123456", includereporting false, arractiontypes \["monetate\ action\ omnichannelrecommendation"] ) on { res in if res status == 200 { self handlerecommendations(res res) } else { } } private func handlerecommendations(res apiresponse) { let data = json(res data as any) for item in data\["data"]\["responses"] arrayvalue { if item\["requestid"] string == res requestid { for oneaction in item\["actions"] arrayvalue { if oneaction\["component"] string == "ios inapp recs" { recproductarr removeall() self reclabel ishidden = false self reclineseperatorview\ ishidden = false self collectionview\ ishidden = false for productdict in oneaction\["items"] arrayvalue { for cat in categorygeneratorforproducts categories { for prdct in cat products { if prdct pid == productdict\["id"] rawvalue as! string { prdct rectoken = productdict\["rectoken"] rawvalue as? string prdct recsetid = productdict\["recsetid"] rawvalue as? double prdct affinity = productdict\[" affinity"] rawvalue as? double prdct rawaffinity = productdict\[" rawaffinity"] rawvalue as? double prdct slotindex = productdict\["slotindex"] rawvalue as? int recproductarr append(prdct) } } } } self collectionview\ reloaddata() dispatchqueue main asyncafter( deadline now() + 2 0, execute { self collectionview\ reloaddata() }) return } } } } } recommendations action full example 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\ omnichannelrecommendation" }, true); // once you receive the responsedata from getactionsdata, use the handlerand 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(); you must handle the getactions method in a thread or asynctask to update the main thread, you can use either handler or runonuithread this full code example uses thread and handler to update the main thread and change the ui