2011-03-29 46 views
0

我终于写了一个脚本来获取谷歌分析数据,然后插入到谷歌可视化API来创建一个图形,像在实际的遗传算法网站。但由于某种原因,图表中的结果是完全正确的,但与此案例中的访问者数量相关的日期总是提前一个月。例如,今天我有10位访问者(3月29日),它表明,但是当我把它悬停在它上面时,它说4月29日。使用谷歌分析(G)API和谷歌可视化 - 显示数据,但从错误的月份

我正在使用默认的PHP(G)API。代码有点冗长,但不应该难以理解。

任何帮助表示赞赏!

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> 

回答

2

我也有这个问题。这是一个JavaScript问题。 (1月为0,2月为1),而PHP(和Google Analytics)则有1个月份(1月份为1)。所以,数据不匹配。