如何使用Analytics API中的数据将相同的数字行传递给Google Charts API

时间:2016-09-01 20:16:18

标签: php arrays google-analytics google-visualization google-analytics-api

我正在尝试从Analytics API获取一些数据,然后使用Google Charts API显示区域图表。

以下数组包含Analytics API v4中两天的数据。屏幕截图显示了如何在Query Explorer中的表中返回/呈现数据。

screenshot

//Using the JavaScript language, have the function SimpleSymbols(str) take the str parameterbeing passed and determine if it is an acceptable sequence by either return in the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
//loop through Array
//Determine if letter is surrounded by +
//If yes continue on and return true  
//If no break loop and return false

function SimpleSymbols(str){
    str = str.split('');
    var pass = null;
    function finder(char){
        if (char.length === 1 && char.match(/[a-z]/i)){
            var firstPlus = str.indexOf(char)- 1;
            var secondPlus = str.indexOf(char)+ 1;
            console.log(str[firstPlus]);
            if (str[firstPlus] === '+' && str[secondPlus] === '+'){
                pass = 'true';
            } else {
                pass = 'false'
                break;
            }
        }
     }
    str.find(finder);
    return pass
 }

SimpleSymbols('++d+===+c++==a++q++');

我正在尝试将默认频道分组和会话数据绘制为30天的区域图表。图表接受的数据格式如下:

<?php
    $results = [
      0 => 
      [
        'dimensions' => 
        [
          0 => 'Organic Search',
          1 => '20160831',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '13',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      1 => 
      [
        'dimensions' => 
        [
          0 => 'Social',
          1 => '20160830',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '7',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      2 => 
      [
        'dimensions' => 
        [
          0 => 'Organic Search',
          1 => '20160830',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '6',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      3 => 
      [
        'dimensions' => 
        [
          0 => 'Social',
          1 => '20160831',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '6',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      4 => 
      [
        'dimensions' => 
        [
          0 => 'Referral',
          1 => '20160831',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '3',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      5 => 
      [
        'dimensions' => 
        [
          0 => 'Referral',
          1 => '20160830',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '2',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
      6 => 
      [
        'dimensions' => 
        [
          0 => 'Direct',
          1 => '20160830',
        ],
        'metrics' => 
        [
          0 => 
          [
            'values' => 
            [
              0 => '1',
              1 => '0',
              2 => '0.0',
            ],
          ],
        ],
      ],
    ];

?>

这是我的代码,试图将API数据放入图表的数组中。

[
  [
    {
      "label": "Day",
      "id": "day"
    },
    {
      "label": "Organic Search",
      "id": "organic_search",
      "type": "number"
    },
    {
      "label": "Referral",
      "id": "referral",
      "type": "number"
    },
    {
      "label": "Social",
      "id": "social",
      "type": "number"
    },
    {
      "label": "Direct",
      "id": "direct",
      "type": "number"
    }
  ],
  [
    "20160831",
    "13",
    "3",
    "0",
    "6"
  ],
  [
    "20160830",
    "1",
    "6",
    "2",
    "7"
  ]
]

我遇到的问题是API并不总是每天返回完全相同数量的频道分组。

例如,8月31日没有直接的流量会话数据。当它按原样传递给图表时,它会失败,因为它总是希望值总是在数组中具有相同数量的项。

如果缺少任何Channel Grouping Session数据,我怎么能添加一个空值,以便Area Charts数组每天总能收到相同数量的值?

2 个答案:

答案 0 :(得分:0)

尝试使用null

        if ($date == $row['dimensions'][1]) {
            $val = $row['metrics'][0]['values'];
            array_push($val, $row['dimensions'][0]);
            array_unshift($val, $date);
            $values[] = $val;
        } else {
            $val = null;
            array_push($val, $row['dimensions'][0]);
            array_unshift($val, $date);
            $values[] = $val;
        }

答案 1 :(得分:0)

如果有人遇到同样的问题,我就是这样做的。

<?php

function getAreaChartValues($response) 
{
    // Get the rows of the response
    $rows = $response->toSimpleObject()->reports[0]['data']['rows'];

    $dimensions = [];

    $results = [];
    foreach ($rows as $row) {

        // Add all the dimensions to the dimensions array
        $dimensions[] = $row['dimensions'][0];

        $date = isset($row['dimensions'][1]) ? $row['dimensions'][1] : '';
        $ValueType = $row['dimensions'][0];
        $value = $row['metrics'][0]['values'][0];

        if (!isset($results[$date])) {
            $results[$date] = [];
        }

        $results[$date][$ValueType] = $value;
    }

    ksort($results);

    $dimensions = array_unique($dimensions);

    // Check for any non existant value for a dimension 
    // And add empty values
    foreach ($dimensions as $dimension) {
        foreach ($results as &$result) {
            if (!array_key_exists($dimension, $result)) {
                $result[$dimension] = 0;
            }
            ksort($result);
        }
    }

    return $results;
}