Search code examples
google-analyticsgoogle-analytics-apigoogle-analytics-filters

Google Analytics API return multiple landing pages data


Can anyone help me out with this? I need to use the GA api to return data of multiple landing pages. Data such as page views, bounce rate, avg time on page, etc.

So far I've managed to return the data for 1 page when this is set in the filters. Is there anyway to do this for multiple pages in 1 request?

// Google API Library
(function(w,d,s,g,js,fjs){
    g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
    js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
    js.src='https://apis.google.com/js/platform.js';
    fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};
}(window,document,'script'));


gapi.analytics.ready(function() {

    var CLIENT_ID = clientID
    var VIEW_ID = viewID;

    gapi.analytics.auth.authorize({
        container: 'auth-button',
        clientid: CLIENT_ID
    });


    var query = {
        ids: VIEW_ID,
        metrics: 'ga:sessions,ga:pageviews,ga:avgTimeOnPage,ga:bounceRate',
        dimensions: 'ga:date',
        filters: 'ga:pagePath=@page1' //<-- page name to search for
    }

    var report = new gapi.analytics.report.Data({ query });
    report.on('success', function(response) {
        console.log(response);      
    });

    report.execute();
});

Solution

  • Add ,ga:pagePath=@page2 in filters for get also the data of the second page and add ga:pagePath in dimensions for separate the two pages in the result.

            var query = {
                ids: VIEW_ID,
                metrics: 'ga:sessions,ga:pageviews,ga:avgTimeOnPage,ga:bounceRate',
                dimensions: 'ga:date,ga:pagePath',
                filters: 'ga:pagePath=@page1,ga:pagePath=@page2' //<-- page name to search for
            }