askmike
askmike

Reputation: 1960

How to retrieve total view count of large number of pages combined from the GA API

We are interested in the statistics of the different pages combined from the Google Analytics core reporting API. The only way I found to query statistics multiple pages at the same is by creating a filter like so:

ga:pagePath==page?id=a,ga:pagePath==page?id=b,ga:pagePath==page?id=c

And this get escaped inside the filter parameter of the GET query.

However when the GET query gets over 2000 characters I get the following response:

414. That’s an error.

The requested URL /analytics/v3/data/ga... is too large to process. That’s all we know.

Note that just like in the example call the only part that is different per page is a GET parameter in the pagePath, but we have to OR a new filter specifying both the metric (pagePath) as well as the part of the path that is always identical.

Is there any way to specify a large number of different pages to query without hitting this limit in the GET query (I can't find any documentation for doing POST requests)? Or are there alternatives to creating batches of a max of X different pages per query and adding them up on my end?

Upvotes: 0

Views: 1193

Answers (2)

Kamran Shahid
Kamran Shahid

Reputation: 4124

I am sharing a sample code where you can fetch more then 10,000 record data via help of Items PerPage

private void GetDataofPpcInfo(DateTime dtStartDate, DateTime dtEndDate, AnalyticsService gas, List<PpcReportData> lstPpcReportData, string strProfileID)
        {
            int intStartIndex = 1;
            int intIndexCnt = 0;
            int intMaxRecords = 10000;

            var metrics = "ga:impressions,ga:adClicks,ga:adCost,ga:goalCompletionsAll,ga:CPC,ga:visits";
            var r = gas.Data.Ga.Get("ga:" + strProfileID, dtStartDate.ToString("yyyy-MM-dd"), dtEndDate.ToString("yyyy-MM-dd"),
                                    metrics);
            r.Dimensions = "ga:campaign,ga:keyword,ga:adGroup,ga:source,ga:isMobile,ga:date";
            r.MaxResults = 10000;            
            r.Filters = "ga:medium==cpc;ga:campaign!=(not set)";

            while (true)
            {
                r.StartIndex = intStartIndex;
                var dimensionOneData = r.Fetch();
                dimensionOneData.ItemsPerPage = intMaxRecords;

                if (dimensionOneData != null && dimensionOneData.Rows != null)
                {
                    var enUS = new CultureInfo("en-US");
                    intIndexCnt++;

                    foreach (var lstFirst in dimensionOneData.Rows)
                    {
                        var objPPCReportData = new PpcReportData();

                        objPPCReportData.Campaign = lstFirst[dimensionOneData.ColumnHeaders.IndexOf(dimensionOneData.ColumnHeaders.FirstOrDefault(h => h.Name == "ga:campaign"))];
                        objPPCReportData.Keywords = lstFirst[dimensionOneData.ColumnHeaders.IndexOf(dimensionOneData.ColumnHeaders.FirstOrDefault(h => h.Name == "ga:keyword"))];                       

                        lstPpcReportData.Add(objPPCReportData);
                    }
                    intStartIndex = intIndexCnt * intMaxRecords + 1;
                }
                else break;
            }
        }

Only one thing is problamatic that your query length shouldn't exceed around 2000 odd characters

Upvotes: 0

Pete
Pete

Reputation: 1862

Instead of using ga:pagePath as part of a filter you should use it as a dimension. You can get up to 10,000 rows per query this way and paginate to get all results. Then parse the results client side to get what you need. Additionally use a filter to scope the results down if possible based on your site structure or page names.

Upvotes: 2

Related Questions