9

I am accessing the Google analytics API with PHP which works on my end but I'd love to filter the results a bit further. Right now I am using:

$OBJresult = $analytics -> data_ga -> get(
    'ga:' . $profilID,
    '2012-01-01',
    date( "Y-m-d" ),
    'ga:visits',
    array(
        'dimensions' => 'ga:pagePath',
        'metrics' => 'ga:pageviews',
        'sort' => '-ga:pageviews',
        'max-results' => '25'
    )
);

Currently this returns a set of 25 pages sorted by its hits. I would love to restrict the results to a specific path within the server. So e.g. only query domain.com/news and only see what the most hit news pages are. I can filter with PHP but rather have the query as specific as possible.

Thanks for the help

3 Answers 3

19

Use the filters option.

$OBJresult = $analytics->data_ga->get(
    'ga:' . $profilID,
    '2012-01-01',
    date("Y-m-d"),
    'ga:visits',
    array(
        'filters' => 'ga:pagePath==/news',
        'dimensions' => 'ga:pagePath',
        'metrics' => 'ga:pageviews',
        'sort' => '-ga:pageviews',
        'max-results' => '25'
    )
);

See here for the list of page tracking dimensions you can filter on.

6
  • thank you! I actually found using the == insteat of =@ works better for me but same problem here: can I filter the absolute address? so domain.com/interviews/article/news would not be found in the result for 'filters' => 'ga:pagePath==/news',?
    – Dominik
    May 24, 2013 at 7:54
  • == should be an exact match, =@ looks for anything that contains the expression. See developers.google.com/analytics/devguides/reporting/core/v3/… for all the operators you can use in filters.
    – Barmar
    May 24, 2013 at 8:05
  • yeah well thats the problem. this now returns nothing as /news alone is not a page but domain.com/news/something is.... so I guess I am looking for something like 'filters' => 'ga:pagePath==/news*',
    – Dominik
    May 24, 2013 at 8:10
  • 1
    ended up using 'filters' => 'ga:pagePath=~^/news/', thanks again!
    – Dominik
    May 24, 2013 at 8:14
  • 3
    You might also want to look at ga:pagePathLevel1, which just matches the first directory of the path.
    – Barmar
    May 24, 2013 at 8:27
11

You need to use the filters string to say "if path includes /news" which can be done as follows:

$OBJresult=$analytics->data_ga->get(
    'ga:'.$profilID,
    '2012-01-01',
    date("Y-m-d"),
    'ga:visits',
    array(
        'filters' => 'ga:pagePath=@/news',
        'dimensions' => 'ga:pagePath',
        'metrics' => 'ga:pageviews',
        'sort' => '-ga:pageviews',
        'max-results' => '25'));

The answer supplied by Barmar will only find an exact match for the /news page.

1
  • thats it really. thanks! though this will look into the url and find all urls that have "/news" in its path. domain.com/news/article and domain.com/interviews/article/news alike... is there a way to limit to domain.com/news/?
    – Dominik
    May 24, 2013 at 6:18
0

Reporting V4 example that may be useful. Thank god for these queries, their objects and poor documentation can cause severe ass cancer..

function segmentRequest(&$analyticsreporting) {

$query = [
        "viewId" => "XXXXXXX",
        "dateRanges" => [
            "startDate" => "2018-02-01",
            "endDate" => "2018-02-15"
        ],
        "metrics" => [
            "expression" => "ga:pageviews"
        ],
        "dimensions" => [
            "name" => "ga:pagepath"
        ],
        "dimensionFilterClauses" => [
            'filters' => [
                "dimension_name" => "ga:pagepath",
                "operator" => "EXACT",
                "expressions" => "/en/some_cool_page.php"
            ]
        ]
    ];

  // Call the batchGet method.
  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $query) );
  $response = $analyticsreporting->reports->batchGet( $body );

  printResults($response->getReports());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.