Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

obtain demographic data with google analytics api

im trying to obtain some demographic data from the google analytics api, my intention is to integrate this on a cms to generate reports, etc... is this possible?

if it is, is there someone who knows a tutorial or something? i have used the examples provided and i get some sessions that have been in the last 7 day period. But nothing besides that. If that is not possible, what kind of things i can obtain with this api?

here is what i have tried:

function getResults(&$analytics, $profileId) {
  return $analytics->data_ga->get(
      'ga:' . $profileId,
      '7daysAgo',
      'today',
      'ga:sessions');
}

thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can obtain everything that's listed in the Dimensions and Metrics Reference. This includes ga:userAgeBracket and ga:userGender for demographic information.

To include additional metrics (numerical data) you put it as a parameter where you now have ga:sessions (separate multiple metrics with a comma). You need at least one metric for a query to work.

To add dimensions (i.e. categorical data) you need to pass an options array to your query that has a key/value pair for dimensions. This may also include additional options like filters or sort options, see the example here..

$optParams = array(
      'dimensions' => 'ga:userAgeBracket,ga:userGender'
);

return $analytics->data_ga->get(
      'ga:' . $profileId,
      '7daysAgo',
      'today',
      'ga:sessions',
      $optParams
);

This for version 3 of the API. If you are just getting started an have no legacy code to maintain you might as well start with there current version (v4).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...