0
Google 애널리틱스 데이터를 가져 오기 위해 스크립트를 작성한 후이를 Google 시각화 API에 삽입하여 실제 GA 웹 사이트와 같은 그래프를 만들었습니다 . 그러나 어떤 이유로 그래프의 결과는 완벽하지만이 경우 방문객 수와 관련된 날짜는 항상 한 달 앞입니다. 예를 들어 오늘 저는 10 명의 방문자 (3 월 29 일)가 있었지만 그걸 보여 줬습니다. 그러나 내가 그것에 마우스를 가져 가면 4 월 29 일이됩니다.Google 애널리틱스 (G) API 및 Google 시각화 사용 - 잘못된 월의 데이터 표시
기본 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>