简体   繁体   中英

Google Analytics Custom Dimension Page View

I have the following working code and I wanted to see the the console log of the page view for each custom dimension I have.

var report = new gapi.analytics.report.Data({
  query: {
    'ids': 'ga:XXXXXX',
    'metrics': 'ga:pageviews',
    'dimensions': 'ga:dimension1',
    'start-date': '7daysAgo',
    'end-date': 'yesterday',
  }
});

// Runs the query.
report.execute();

// Specifies the callback function  to be run when the query succeeds.
report.on('success', function(response) {

  // Logs the entire response object.
  console.log(response);

  // Logs just the total pageviews.
  console.log(response.totalsForAllResults['ga:pageviews']);
});

You're going to have to do multiple queries to get these counts. Since dimensions can overlap (eg ga:browser=Chrome and ga:date=20150821 ) or not overlap (eg ga:browser=Internet Explorer and ga:operatingSystem=Windows ), it would be much more complicated to parse the result and tally the counts yourself. Just do a separate query for each dimension you have.

Here's an example:

var baseQueryData = {
  'ids': 'ga:XXXXXX',
  'metrics': 'ga:pageviews',
  'start-date': '7daysAgo',
  'end-date': 'yesterday',
}

function runQuery(dimension, callback) {
  var report = new gapi.analytics.report.Data({query: baseQueryData});
  report.set({query: {dimensions: dimension}});
  report.on('success', callback);
  report.execute();
}

function logResult(response) {
  var dimension = response.query.dimensions;
  var count = response.totalsForAllResults['ga:pageviews'];
  console.log(dimension, count);
}

runQuery('ga:dimension1', logResult);
runQuery('ga:dimension2', logResult);
runQuery('ga:dimension3', logResult);

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