Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google analytics api query a specific url

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

like image 361
Dominik Avatar asked May 24 '13 05:05

Dominik


People also ask

What is query parameter in Google Analytics?

Query parameter targeting explicitly targets values that occur in the query string of a URL. Query parameters are found between the question mark ( ? ) and hash mark ( # ), for example: https://www.example.com/store/landing?


1 Answers

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.

like image 124
David Shaw Avatar answered Oct 07 '22 21:10

David Shaw