简体   繁体   English

使用Google Analytics(G)API和Google Visualization - 显示数据,但是从错误的月份开始

[英]Using Google Analytics (G)API and Google Visualization - Showing data, but from the wrong month

I finally wrote a script to get Google Analytics data then insert it into the Google Visualization API to create a graph like on the actual GA website. 我最后编写了一个脚本来获取Google Analytics数据,然后将其插入到Google Visualization API中,以便在实际的GA网站上创建一个图表。 But for some reason the results on the graph are perfectly right, but the date associated with the number of visitors in this case is always a month ahead. 但由于某种原因,图表上的结果是完全正确的,但在这种情况下与访客数量相关的日期总是提前一个月。 For example today I had 10 visitors (March 29th) and it shows that, but when I hover over it, it says April 29th. 例如,今天我有10名访客(3月29日),它显示了这一点,但当我将鼠标悬停在它上面时,它表示4月29日。

I'm using the default PHP (G)API. 我正在使用默认的PHP(G)API。 The code's a bit long-winded, but it shouldn't be hard to understand. 代码有点啰嗦,但不应该难以理解。

Any help is appreciated! 任何帮助表示赞赏!

try {
    // create an instance of the GoogleAnalytics class using your own Google {email} and {password}
    $ga = new gapi($settings_data->analytics_username, $settings_data->analytics_password);

    $curr_date = date("Y-m-d", time());

    $new_date = strtotime('-1 month', strtotime($curr_date));

    // set the Google Analytics profile you want to access - format is 'ga:123456';
    $report = $ga->requestReportData($settings_data->analytics_profile_id, array('year', 'month', 'day'),array('visits'), null, null, date("Y-m-d", $new_date), $curr_date);

    //echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';

} catch (Exception $e) { 
    print 'Error: ' . $e->getMessage();

    echo '<p>If you have not yet set your Google Analytics account information in the settings panel, you will see an error here.</p>';
}
?>

<?php

$j_input = "";

foreach ($ga->getResults() as $result) { 

    $date = explode(' ', $result);

    $visits = $result->getVisits();

    $j_input .= "[new Date($date[0], $date[1], $date[2]), $visits],";

    $output = substr($j_input, 0, -1);

} ?>

<script type='text/javascript' src='https://www.google.com/jsapi'></script>


<script type='text/javascript'>
    google.load('visualization', '1', {'packages':['annotatedtimeline']});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Visits');

        data.addRows([
            <?php echo $output; ?>
        ]);

        var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('analytics'));
        chart.draw(data, {displayAnnotations: true});
    }
</script>

<div id="analytics" style="width: 100%; height: 200px;"></div>

I had this problem, too. 我也有这个问题。 It's a JavaScript issue. 这是一个JavaScript问题。

JavaScript months are zero indexed (January is 0, February is 1), whereas PHP (and Google Analytics) have 1-indexed months (January is 1). JavaScript月份为零索引(1月为0,2月为1),而PHP(和Google Analytics)的索引为1个月(1月为1)。 So, the data isn't matching up. 因此,数据不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM