STEP 1
Add the below 2 urls in the api.txt file, located in the company directory (selfserve/XXX) (requires shell access, the Forsta support team can do it, too):
better-params-api.rose.conceptsauce.io
public-survey-data-api.rose.conceptsauce.io
Knowledge base link - https://decipher.zendesk.com/hc/en-us/articles/360010277973-Generic-API-Call
STEP 2
Add the below 2 to your sample source:
<var name="param_id" required="1"/> <var name="respondent_id" unique="1"/>
STEP 3
Add the below code to your survey:
Add the below code to your survey: <exec cond="not gv.isSST()"> surveyDir = "https://XXXX.XXXX.com/survey/%(first)s?state=[state]" % {"first": gv.survey.path} redirectTarget = "https://survey.rose.conceptsauce.io/?param_id=%(first)s&respondent_id=%(second)s&link=%(third)s" % {"first": param_id, "second": respondent_id, "third": surveyDir}
The survey url, the XXXX parts should be replaced with yours (could be seen by opening the survey)
STEP4
Redirecting:
suspendExternal(redirectTarget) </exec>
STEP 5
JSON object needed for first API request:
<textarea label="apiRequest_1" where="execute,survey,report"> <title>hidden: api request 1</title> <exec> createJson = { "event_type": "params_crud", "action": "get_params", "data": { "params_id": param_id } } thisQuestion.val = str(createJson).replace("'",'*').replace('*','"') </exec> </textarea> <suspend/>
STEP 6
The first API request, using Decipher’s build in logic node:
<logic label="logic_apiRequest_1" api:data="apiRequest_1.unsafe_val" api:method="POST" api:url="https://better-params-api.rose.conceptsauce.io/api" uses="api.1"/> <suspend/>
STEP 7
If API call successful - create the needed JSON object for the second API request:
<textarea label="hResult_apiRequest_1" where="execute,survey,report"> <title>hidden: api request 1 response</title> <exec> findApi = logic_apiRequest_1 if findApi.status == 200: response = findApi.r survey_id = str(response["data"]["params"]["survey_id"]) createJson = { "event_type": "survey_data_request", "action": "get_shop_shelf_summary_purchases", "data": { "target": { "survey_id": survey_id, "respondent_id": respondent_id, "block_id": "shelf" } } } thisQuestion.val = str(createJson).replace("'",'*').replace('*','"') </exec> </textarea> <suspend/>
STEP 8
Second API request:
<logic label="logic_apiRequest_2" cond="hResult_apiRequest_1.val not in ['', None]" api:data="hResult_apiRequest_1.unsafe_val" api:method="POST" api:url="https://public-survey-data-api.rose.conceptsauce.io/api" uses="api.1"/>
STEP 9
Saving the total_unique_sku
value:
<number label="hTotal_Unique_Sku" size="3" where="execute,survey,report"> <title>Hidden: total_unique_sku</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r thisQuestion.val = response["data"]["basket_summary"]["total_unique_sku"] </exec> </number> <suspend/>
STEP 10
Saving the total_value
of basket:
<float label="hTotal_Value" size="3" where="execute,survey,report"> <title>Hidden: total_value</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r thisQuestion.val = response["data"]["basket_summary"]["total_value"] </exec> </float> <suspend/>
STEP 11 - Final step 🥳
Saving each selected product:
<checkbox label="hSku_List" where="execute,survey,report"> <title>Hidden: sku_list</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r findItems = response["data"]["basket_summary"]["sku_list"].split(",") for x in findItems: try: thisQuestion.attr("r" + x.replace(" ", "")).val = 1 except: pass </exec> <row label="r5053827111157">5053827111157</row> <row label="r5053827114332">5053827114332</row> <row label="r9417986939919">9417986939919</row> <row label="r9300605088085">9300605088085</row> <row label="r9300605043855">9300605043855</row> <row label="r9310055537279">9310055537279</row> <row label="r9310055536593">9310055536593</row> <row label="r9310055100046">9310055100046</row> <row label="r9310055537194">9310055537194</row> <row label="r9310055790919">9310055790919</row> <row label="r5414624321000">5414624321000</row> <row label="r9310055100220">9310055100220</row> </checkbox> <suspend/>
And here is all the code in one script:
<exec cond="not gv.isSST()"> surveyDir = "https://XXXX.XXXX.com/survey/%(first)s?state=[state]" % {"first": gv.survey.path} redirectTarget = "https://survey.rose.conceptsauce.io/?param_id=%(first)s&respondent_id=%(second)s&link=%(third)s" % {"first": param_id, "second": respondent_id, "third": surveyDir} suspendExternal(redirectTarget) </exec> <textarea label="apiRequest_1" where="execute,survey,report"> <title>hidden: api request 1</title> <exec> createJson = { "event_type": "params_crud", "action": "get_params", "data": { "params_id": param_id } } thisQuestion.val = str(createJson).replace("'",'*').replace('*','"') </exec> </textarea> <suspend/> <logic label="logic_apiRequest_1" api:data="apiRequest_1.unsafe_val" api:method="POST" api:url="https://better-params-api.rose.conceptsauce.io/api" uses="api.1"/> <suspend/> <textarea label="hResult_apiRequest_1" where="execute,survey,report"> <title>hidden: api request 1 response</title> <exec> findApi = logic_apiRequest_1 if findApi.status == 200: response = findApi.r survey_id = str(response["data"]["params"]["survey_id"]) createJson = { "event_type": "survey_data_request", "action": "get_shop_shelf_summary_purchases", "data": { "target": { "survey_id": survey_id, "respondent_id": respondent_id, "block_id": "shelf" } } } thisQuestion.val = str(createJson).replace("'",'*').replace('*','"') </exec> </textarea> <suspend/> <logic label="logic_apiRequest_2" cond="hResult_apiRequest_1.val not in ['', None]" api:data="hResult_apiRequest_1.unsafe_val" api:method="POST" api:url="https://public-survey-data-api.rose.conceptsauce.io/api" uses="api.1"/> <number label="hTotal_Unique_Sku" size="3" where="execute,survey,report"> <title>Hidden: total_unique_sku</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r thisQuestion.val = response["data"]["basket_summary"]["total_unique_sku"] </exec> </number> <suspend/> <float label="hTotal_Value" size="3" where="execute,survey,report"> <title>Hidden: total_value</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r thisQuestion.val = response["data"]["basket_summary"]["total_value"] </exec> </float> <suspend/> <checkbox label="hSku_List" where="execute,survey,report"> <title>Hidden: sku_list</title> <exec> findApi = logic_apiRequest_2 if findApi.status == 200: response = findApi.r findItems = response["data"]["basket_summary"]["sku_list"].split(",") for x in findItems: try: thisQuestion.attr("r" + x.replace(" ", "")).val = 1 except: pass </exec> <row label="r5053827111157">5053827111157</row> <row label="r5053827114332">5053827114332</row> <row label="r9417986939919">9417986939919</row> <row label="r9300605088085">9300605088085</row> <row label="r9300605043855">9300605043855</row> <row label="r9310055537279">9310055537279</row> <row label="r9310055536593">9310055536593</row> <row label="r9310055100046">9310055100046</row> <row label="r9310055537194">9310055537194</row> <row label="r9310055790919">9310055790919</row> <row label="r5414624321000">5414624321000</row> <row label="r9310055100220">9310055100220</row> </checkbox> <suspend/>
Also attached is a JPG to explain all the code in one place: