Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analytics Reporting API - Get Total Pageviews ALL Pages

Does Google Analytics Reporting API v4 have a built in function to give total pageviews across all pages defined by filter? For instance, when I have this:

$pageviews = new Google_Service_AnalyticsReporting_Metric();
$pageviews->setExpression("ga:pageviews");
$pageviews->setAlias("pageviews");
//Create the source dimension.
$source = new Google_Service_AnalyticsReporting_Dimension();
$source->setName("ga:pagePath");
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName("ga:pagePath");
$dimensionFilter->setOperator("BEGINS_WITH");
$dimensionFilter->setExpressions("/article/deleted-x-men-apocalypse-scene-awesome-tribute-80s");

$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($dimensionFilter));
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setDimensions(array($source));
$request->setDimensionFilterClauses(array($dimensionFilterClause));
$request->setMetrics(array($pageviews));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );

I get this as result:

 ga:pagePath:
 /article/deleted-x-men-apocalypse-scene-awesome-tribute-80s

 pageviews: 6

 ga:pagePath:
 /article/deleted-x-men-apocalypse-scene-awesome-tribute-80s/

 pageviews: 4458

 ga:pagePath:
 /article/deleted-x-men-apocalypse-scene-awesome-tribute-80s/?b_comment_id=fbc_1181872881884957_1181963985209180_1181963985209180

 pageviews: 3

 ga:pagePath:
 /article/deleted-x-men-apocalypse-scene-awesome-tribute-80s/+ sadsHost
 + ":/api.avidadserver.com/api/Asms/Preview/?id=579c5f501ee0530bcc900738&pid=57c388e11ee0530a90c94fd9&w=300&h=250&rnd=[CACHE-BUSTING-ID-HERE]
 width=

 pageviews: 1

It's picking up all variations with URL queries included. I'd like the total pageviews of this whole set, so I don't have to add them myself on my end. Or is there a way to tell the API to just ignore the ?URL queries, and just have them all included as the primary URL?

All feedback appreciated. I only use "BEGINS_WITH" since there are a couple variations of the URL. Would nice to just be EXACT and make sure to include the variations.

Cheers Ryan

like image 591
user3273784 Avatar asked Mar 07 '26 10:03

user3273784


1 Answers

Usage of dimensions result a breakdown of your selected metrics, by the dimensions provided. Generally, filters can be applied without using them as dimensions. This part of the code is responsible for adding dimensions to your query:

$request->setDimensions(array($source));

In your case, this was set up earlier here:

//Create the source dimension.
$source = new Google_Service_AnalyticsReporting_Dimension();
$source->setName("ga:pagePath");

I'd suggest to remove at least the actual assignment of dimensions, or even the whole setup of $source variable to get the desired result, if $source will not be used at all.

like image 113
kgrg Avatar answered Mar 10 '26 04:03

kgrg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!