如何获取活跃访问者,会话,页面浏览量和退回基于设备(桌面,移动和应用程序)

时间:2015-06-02 11:34:27

标签: php google-analytics-api

使用google api获取活跃访问者,会话,页面浏览量和退回(不基于设备); 我的代码是这样的:

<?php

$strRadioType = "RealTime";
//$strRadioType = $_GET['rdReportType'];
if($strRadioType != "RealTime"){
    $dtFromFormat = $_GET['dtFromDate'];
    $dtToFormat = $_GET['dtToDate']; 
}else{
    $dtFromFormat = date("Y-m-d");
    $dtToFormat = date("Y-m-d");
}
set_include_path('src/' . PATH_SEPARATOR . get_include_path());
require_once "Google/Client.php";
require_once 'Google/Service/Analytics.php';
$CLIENT_ID = '*******';
$CLIENT_EMAIL = '****@developer.gserviceaccount.com';
$SCOPE = 'https://www.googleapis.com/auth/analytics.readonly';
$KEY_FILE = 'key.p12';
$GA_VIEW_ID = 'ga:66379307';

$client = new Google_Client();
$client->setClientId($CLIENT_ID);
$client->setAssertionCredentials(
    new Google_Auth_AssertionCredentials(
        $CLIENT_EMAIL,
        array($SCOPE),
        file_get_contents($KEY_FILE)
    )
 );

  $service = new Google_Service_Analytics($client);
  try {
    if($strRadioType == "RealTime"){
        $result1 = $service->data_realtime->get(
            $GA_VIEW_ID,
            'rt:activeVisitors' 
        );
    }
    $result = $service->data_ga->get(
        $GA_VIEW_ID,
         $dtFromFormat,
         $dtToFormat,
        'ga:pageViews'
    );

    $result2 = $service->data_ga->get(
        $GA_VIEW_ID,
         $dtFromFormat,
         $dtToFormat,
        'ga:bounces'
    );

    $result3 = $service->data_ga->get(
        $GA_VIEW_ID,
         $dtFromFormat,
         $dtToFormat,
        'ga:sessions'
    );
    echo "<table border='1' style='font-size:12px;'>";
    if($strRadioType == "RealTime"){
        echo "<tr><th>Date</th>";
        echo "<th>Total Active Visitors</th>";
    }else{
        echo "<tr><th>From Date</th><th>To Date</th>";
    }
    echo "<th>Total Pageviews</th><th>Total Bounce</th><th>Total                    Sessions</th></tr>";
    if($strRadioType == "RealTime"){
        echo "<tr><td>". $dtFromFormat."</td>";
        echo "<td>".$result1->totalsForAllResults['rt:activeVisitors']."</td>";
    }else{
        echo "<tr><td>". $dtFromFormat."</td><td>". $dtToFormat."</td>";
    }
    echo "<td>".$result->totalsForAllResults['ga:pageViews']."</td>";
    echo "<td>".$result2->totalsForAllResults['ga:bounces']."</td>";
    echo "<td>".$result3->totalsForAllResults['ga:sessions']."</td></tr></table>";
   // echo "Total Device Category : ".$result4-      >totalsForAllResults['rt:deviceCategory']."<br/>";
} catch(Exception $e) {
    var_dump($e);
}

&GT;

此代码工作正常。但是这里提取的总数据不是基于设备(桌面,移动和应用程序)。

获得如下输出: enter image description here

所以,最后我关注的是需要根据设备(桌面,移动和应用程序)获取这些数据。

有人帮助我......

0 个答案:

没有答案