doukan3504 2018-02-08 21:26
浏览 60
已采纳

Optimizely API - “缺少必要的现场访客”

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:

Optimizely X API overview

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?

  • 写回答

1条回答 默认 最新

  • dongroufan6846 2018-02-08 21:53
    关注

    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'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统