I'm trying to use the Optimizely API for the first time using a webhook that gets triggered by another tracking platform (WhatConverts). The goal is to track phone calls since Optimizely natively doesn't.
I'm successfully capturing the data from WhatConverts and writing it to a database to be used later. I'm just having an issue sending it to Optimizely. I followed the API documentation here:
Here is how I'm building out the data based on their demo:
$jsonData = array(
'account_id' => '8585984149',
'project_id' => '8585984149',
'visitors' => array(
'session_id' => '',
'visitor_id' => 'java-lover@example.com',
'snapshots' => array(
'decisions' => array(
'campaign_id' => '8603360066',
'experiment_id' => '8599910077',
'variation_id' => '8602330084'
),
'events' => array(
'entity_id' => '9560823711',
'type' => 'campaign_activated',
'timestamp' => 1491519130343,
'uuid' => '3a427b02-7ae0-4b20-8f02-32cc8a067be4'
),
),
),
'anonymize_ip' => true,
'client_name' => 'Optimizely/event-api-demo',
'client_version' => '1.0.0'
);
Then I json_encode()
it and send it as a POST. However, I'm getting the following error "Missing the required field visitors[] in within the batch json payload".
I also tried json_encode($jsonData, JSON_PRETTY_PRINT)
that I came across while researching, but got the same result.
Finally, I tried this function that I found before encoding it because I read that strings as keys will force it to be an object:
function fix_keys($jsonData) {
$numberCheck = false;
foreach ($jsonData as $k => $val) {
if (is_array($val)) $jsonData[$k] = fix_keys($val); //recurse
if (is_numeric($k)) $numberCheck = true;
}
if ($numberCheck === true) {
return array_values($jsonData);
} else {
return $jsonData;
}
}
Again, same result. Can anybody point me in the right direction to getting this working?
Problem is probably that you sends wrong data in visitors because API expecting array of arrays. Can you try send something like that?
$jsonData = array(
'account_id' => '8585984149',
'project_id' => '8585984149',
'visitors' =>
array(
array(
'session_id' => '',
'visitor_id' => 'java-lover@example.com',
'snapshots' => array(
'decisions' => array(
'campaign_id' => '8603360066',
'experiment_id' => '8599910077',
'variation_id' => '8602330084'
),
'events' => array(
'entity_id' => '9560823711',
'type' => 'campaign_activated',
'timestamp' => 1491519130343,
'uuid' => '3a427b02-7ae0-4b20-8f02-32cc8a067be4'
),
),
)
),
'anonymize_ip' => true,
'client_name' => 'Optimizely/event-api-demo',
'client_version' => '1.0.0'
);