简体   繁体   中英

How to filter by custom parameter in Google Analytics API?

I'm trying to get a list the most visited pages for mu website from Google Analytics API. Currently I have the following code and that returns a list with a title, url and number of visitors of each page:

data_query = service.data().ga().get(**{
    'ids': 'ga:123456789',
    'dimensions': 'ga:pageTitle,ga:pagePath',
    'metrics': 'ga:pageviews',
    'start_date': '2013-12-31',
    'end_date': '2014-04-29',
    'sort': '-ga:pageviews',
})
feed = data_query.execute()

The problem is that all pages of the website can be rendered in Spanish or in English, so as a result I get duplicated rows for the same url, for example:

[
    ['/index/', 'Inicio', 23],
    ['/index/', 'Home', 57],
]

In order to be able to filter results by language I'd to get something like this:

[
    ['/index/', 'Inicio', 23, 'es'],
    ['/index/', 'Home', 57, 'en'],
]

Following Google Analytics instructions, I created a new dimension 'lang' ('dimension1') and in my html template I pass its value dynamically depending on the current language as en or es :

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'xxx', 'yyy');
ga('send', 'pageview');
ga('set', 'dimension1', 'en');   // <----- or 'es'

</script>

But this query

data_query = service.data().ga().get(**{
    'ids': 'ga:123456789',
    'dimensions': 'ga:pageTitle,ga:pagePath,ga:dimension1',
    'metrics': 'ga:pageviews',
    'start_date': '2013-12-31',
    'end_date': '2014-04-29',
    'sort': '-ga:pageviews',
})

aways returns me an empty list.


Update:

It turned out that the dimension was set, but ga was not sending the value correctly, and this code did the thing:

ga('send', 'dimension1', 'en');

Are you sure that here:

ga('set', 'dimension1', 'en');   // <----- or 'es'

you set a dimension?

Maybe you set it like a metric and you call it like a dimension.

Btw, If you are sure that you set a dimension, I believe that is better to set like a metric because this is a descripción of de pagePath.

You already have the dimension of Language in Analytics.

似乎它在这里修复: http//axiacore.com/blog/how-get-list-most-popular-pages-google-analytics-python/也许它也适合你。

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