I'm playing around with a class called the "GA:PI" class, which is alright, and I know it works - but at the moment, my only problem is that I would like to be able to view the total page views, and the total visits on the one single page - not for the entire site. Unfortunately, I can't seem to get this working. Here's the code (yes, 'ga_username', 'ga_password', and 'ga_profile_id' are defined, I just haven't showed them in this code):
$ga = new gapi(ga_email, ga_password);
$filter = 'pagePath =@ ' . $_SERVER['REQUEST_URI'];
$ga->requestReportData(ga_profile_id,array('pagePath'),array('uniquepageviews','visits'),'-visits',$filter);
if ( $data['show_on'] == 1 ) :
if ( is_single() ) : ?>
<table style="display: block; background: #eee; color: #888;">
<tr>
<th style="font-size: 14px; padding: 10px 10px 0;">Total Page Views</th>
开发者_StackOverflow中文版 <th style="font-size: 14px; padding: 10px 10px 0;">Total Visits</th>
<th style="font-size: 14px; padding: 10px 10px 0;">Total Results</th>
</tr>
<tr>
<td style="font-size: 24px; padding: 10px; text-align: center;"><?php echo $ga->getUniquePageviews(); ?></td>
<td style="font-size: 24px; padding: 10px; text-align: center;"><?php echo $ga->getVisits(); ?></td>
<td style="font-size: 24px; padding: 10px; text-align: center;"><?php print $ga->getTotalResults(); ?></td>
</tr>
</table>
<? foreach ( $ga->getResults() as $result ) :
print $result . ' (' . $result->getProfileId() . ')<br />';
endforeach;
endif;
Currently, I'm using it on WordPress, but I have no intention whatsoever of releasing it as a plugin or anything; I'm just developing it for me, and then maybe putting the source on Github or something.
But, either way, any help with helping me out displaying the analytics for the single page would be great. At the moment, the only value that I'm getting returned is 0, and I know that it works because I've returned all of the data on the current page for the entire site - so monthly results - but, the only thing I want to do is display the total results for the currently active page.
Cheers.
I was looking to do something very similar to your needs, I needed the total pageviews and the total visits for a single page URL.
The format of my PHP pages was:
/pagename.php?id=XXX&pa=YY
(YY is a pagenumber variable (numeric) that stores the page inside the article, but I wanted the total number for single article with the id=XXX).
Finally after several tries this code seems to work for me:
$filter = $_SERVER['REQUEST_URI']; //PAGE URI
$filter = preg_replace('/&pa=(\d+)/i', '', $filter ); //REMOVE THE PA=YYY
$filter = 'pagePath =@ ' . $filter; //ADD THE GA:PI FILTER SYNTAX
define('ga_email','email@gmail.com');
define('ga_password','password');
require $_SERVER['DOCUMENT_ROOT'] . '/classes/gapi.class.php'; //YOUR PATH
$ga = new gapi(ga_email,ga_password);
$ga->requestReportData(999999999,array('pagepath'),array('uniquepageviews', 'pageviews', 'visits'),null, $filter ,$start_date=null, $start_date=null, $start_index=1, $max_results=30);
$total_pageviews = 0;
$total_visits = 0;
foreach($ga->getResults() as $result)
{
$total_pageviews = $total_pageviews + $result->getPageviews();
$total_visits = $total_visits + $result->getVisits();
}
echo 'Pageviews:' . $total_pageviews . '<br />';
echo 'Visits:' . $total_visits;
Hope it helps you someway.
Best regards.
P:.
精彩评论