Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

Code Block
languagexml
<var name="param_id" required="1"/>
<var name="respondent_id" unique="1"/>

STEP 3

Add the below code to your survey:

...

The survey url, the XXXX parts should be replaced with yours (could be seen by opening the survey)

STEP4

Redirecting:

Code Block
suspendExternal(redirectTarget)
</exec>

STEP 5

JSON object needed for first API request:

Code Block
<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:

Code Block
languagejs
<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:

Code Block
<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:

Code Block
<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:

Code Block
<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:

Code Block
<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:

Code Block
<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:

Code Block
languagexml
<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&amp;respondent_id=%(second)s&amp;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/>

...