开发者

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

开发者 https://www.devze.com 2023-02-20 09:07 出处:网络
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. But for some reason the results on the graph are

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. 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.

I'm using the default 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 months are zero indexed (January is 0, February is 1), whereas PHP (and Google Analytics) have 1-indexed months (January is 1). So, the data isn't matching up.

0

精彩评论

暂无评论...
验证码 换一张
取 消