简体   繁体   中英

Google Analytics PHP API (GAPI) - Get Direct Traffic Data

I am new at google analytics and am still finding my way around. I am trying to find a way on how to retrieve the data (landing page, visits, pages/visits, avg. visit duration, % new visits, bounce rate) under direct traffic on dashboard(traffic sources-->sources-->direct). I've tried filtering it by using define('filter', 'source == direct'); as their website states but no luck to avail. I've seen a similar post here but I want to deepen my search. Am I missing something?

My current code can only retrieve data from all traffic. .

require_once('gapi.class.php');

define('email', 'email address');
define('password', 'password');
define('profileID', profileID);
define('dimensions', 'source');
define('metrics', 'visits');
define('sortMetric', '-visits');
define('filter', null);
define('startDate', '2013-01-18');
define('endDate', '2013-02-17');
define('startIndex', 1);
define('maxResult', 10);

$ga = new gapi(email, password);
$ga->requestReportData(profileID,dimensions,metrics,sortMetric,filter,startDate,endDate,startIndex,maxResult);

foreach($ga->getResults() as $result){
    echo '<strong>'.$result.'</strong><br />';
    echo 'Source: ' . $result->getSource() . ' ';
    echo 'Visits: ' . $result->getVisits() . '<br /><br />';
}

I found the answer, just want to share it to you guys just in case somebody encounters the same problem.

Basically my mistake was i didn't surround the direct with parenthesis () . I just replaced the define('filter', 'source == direct'); into define('filter', 'source==(direct)'); and viola! I get what I want.

Here's my code just in case you need it, you might notice that I have replaced the dimensions and metrics as i need them to be an array.

require_once('gapi.class.php');

define('email', 'email address');
define('password', 'password');
define('profileID', profileID);
$dimensions = array('landingPagePath');
$metrics = array('visits','pageviewsPerVisit','avgTimeOnSite','percentNewVisits','visitBounceRate');
define('sortMetric', '-visits');
define('filter', 'ga:source==(direct)');
define('startDate', '2013-01-18');
define('endDate', '2013-02-17');
define('startIndex', 1);
define('maxResult', 10);

$ga = new gapi(email, password);
$ga->requestReportData(profileID,$dimensions,$metrics,sortMetric,filter,startDate,endDate,startIndex,maxResult);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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