Google Analytics API V4: includeEmptyRows: 'true', Not Working

情到浓时终转凉″ 提交于 2020-01-24 14:24:30

问题


I'm migrating my app to V4, the thing is that you need setting includeEmptyRows to TRUE, and that's what I have just done. However the empty rows are not populating at all.

function queryReports() {
    gapi.client.request({
        path: '/v4/reports:batchGet',
        root: 'https://analyticsreporting.googleapis.com/',
        method: 'POST',
        body: {
            reportRequests: [
                {
                    **includeEmptyRows: 'true',**
                    //**this above for empty rows

                    viewId: VIEW_ID,
                    dateRanges: [
                        {
                            startDate: startDate0,
                            endDate: endDate0
                        }
                    ],
                    //**below samplimg levels
                    samplingLevel: 'LARGE',
                    //request metrics here
                    metrics: [
                        {
                            expression: 'ga:sessions',
                        }
                        ,
                        {
                            expression:'ga:goalCompletionsAll',
                        },
                        {
                            expression:'ga:transactionRevenue',
                        },
                        {
                            expression:'ga:transactions',
                        },
                    ],
                    //request dimensions here
                    dimensions: [
                        {
                            name:'ga:channelGrouping',
                        },

                        {
                            name:'ga:yearMonth',
                        },
                    ],
                }
            ]
        }
    }).then(displayResults, console.error.bind(console));
}

I'm getting only not empty rows values, so for months where there is no data about any specific channel is simply skipped :/

Not sure what is wrong here, I followed the specification in the documentation but it's just not working at all.

Hope someone can give me a hand,

Many thanks J.


回答1:


The documentation could be a lot better - unfortunately it leads people to believe that you will get rows returned for each combination of dimensions that have zero values. Unfortunately this is not true. If you were to exclude ChannelGrouping from your query, you will indeed get rows for all yearMonth values in your date range, even when there were 0 sessions.

In other words, this works as you'd expect when you ONLY include Date dimensions (and not time dimensions) in your query.

I believe this is because the cardinality of each dimension over each date period is unknown. Would you expect to have a row with 0 sessions for each yearMonth and ChannelGrouping combination? What if you added country and city as a dimension?

It is possible to get the results you're after, just not from a single query against the API. Here is an outline of the steps using Analytics Canvas, but you can perform the same steps on your own using JavaScript (or any language to make the API calls) and SQL.

To get the results you're after make the following 3 queries:

1) yearMonth and sessions with empty rows:

2) channelGrouping and sessions over the time period

3) yearMonth, channelGrouping, and sessions (your query as shown above):

Now perform a cross join on queries 1 and 2, dropping sessions from your output. You will have a table with all combinations of yearMonth and channelGrouping for your time period. Let's call this Table 4.

Next perform a left outer join on Table 4 and Query 3, dropping yearMonth and channelGrouping from Query 3.

You now have Table 5, which has every combination of yearMonth and channelGrouping, values for sessions where there were sessions in that combination, and null values for the rest.

Finally replace the null values with zeros and you've got the dataset you're after.

isnull([Original.sessions], 0)



来源:https://stackoverflow.com/questions/44026186/google-analytics-api-v4-includeemptyrows-true-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!