Using and querying Custom Dimensions in Google Ana

2019-03-20 15:08发布

问题:

I am attempting to query my Analytics (Universal) to receive a list of metrics sorted by a custom dimension.

In July, the Google Analytics API blog announced:

"Developers can use custom dimensions to send unique IDs into Google Analytics, and then use the core reporting API to retrieve these IDs along with other Google Analytics data.

For example, your content management system can pass a content ID as a custom dimension using the Google Analytics tracking code. Developers can then use the API to get a list of the most popular content by ID and display the list of most popular content on their website."

Despite this, I have been unable to retrieve any results from my Analytics. My send function is below:

ga('send', 'pageview', {
  'dimension1':'red'
});

Unfortunately, querying GA using a filter of 'ga:dimension1 == red' does not retrieve any results.

gapi.client.analytics.data.ga.get({
  'ids': 'ga:' + "123456",
  'start-date': '2013-11-10',
  'end-date': '2013-11-20',
  'metrics': 'ga:visits',
  'filters': 'ga:dimension1==red'
}).execute(function(r){console.log(r);});

I have also tried using custom segments to retrieve the data, to no avail:

'segment': 'dynamic::ga:dimension1==red'

The data appears in Custom Reports in GA. How can I access it via the API?

回答1:

You need to include a the dimension in the query. Such as 'dimension': 'ga:dimension1', then use your filter 'ga:dimension1==red'. The Query Explorer is very helpful for testing API requests.



回答2:

hey this is very helpful. thanks so much for the answer. Let me also answer this question with an example: let so for a ga defined dimension, the syntax would look like - ga:landingPagePath=="what ever you want" now say there is a custom dimension called pagepath which is customdimension1 . Hence the syntax will be ga:dimension1=="what ever you want"