【发布时间】:2018-06-15 11:57:04
【问题描述】:
我正在处理嵌套的 Google Analytics 数据以构建查询,我需要取消嵌套 3 个级别才能获得我需要的所有字段,但是一旦我取消嵌套我的 .totals 字段的 SUM() 就太远了高,我猜是因为它们的值被重复了。
我无法在命中级别上识别弹跳,因此我需要使用totals.bounces 来获取此值。
如何调整下面的查询以获得正确的跳出和收入总和以及未嵌套值的总数?
SELECT
customDimension.value AS UserID,
# Visits from this individual
SUM(totals.visits) AS visits,
# Orders from this individual
COUNT(DISTINCT hits.transaction.transactionId) AS orders,
# AOV
SAFE_DIVIDE(SUM(hits.transaction.transactionRevenue)/1000000 , COUNT(DISTINCT hits.transaction.transactionId)) AS AOV,
#Bounces from this individual
IFNULL(SUM(totals.bounces),
0) AS bounces,
IFNULL(SUM(hits.transaction.transactionRevenue)/1000000,
0) AS revenue,
# Conversion rate of the individual
SAFE_DIVIDE(COUNT(DISTINCT hits.transaction.transactionId),COUNT(DISTINCT CONCAT(CAST(fullVisitorId AS STRING),CAST(visitId AS STRING)))) AS conversion_rate,
ROUND(IFNULL(SUM(totals.bounces)/COUNT(DISTINCT CONCAT(CAST(fullVisitorId AS STRING),CAST(visitId AS STRING))),
0),5) AS bounce_rate,
FROM
`MY.DATA.ga_sessions_20*` AS t
CROSS JOIN
UNNEST (hits) AS hits
CROSS JOIN
UNNEST(t.customdimensions) AS customDimension
CROSS JOIN
UNNEST(hits.product) AS hits_product
WHERE parse_date('%y%m%d', _table_suffix) between
DATE_sub(current_date(), interval 7 day) and
DATE_sub(current_date(), interval 1 day)
AND customDimension.index = 2
AND customDimension.value NOT LIKE "true"
AND customDimension.value NOT LIKE "false"
AND customDimension.value NOT LIKE "undefined"
AND customDimension.value IS NOT NULL
GROUP BY
UserID,
hits.eventInfo.eventCategory
【问题讨论】:
-
1) 您没有使用 hits_product,因此您可能需要删除相应的 UNNEST 2) 如果您想要正确的反弹总和 - 您需要想出一些将它们分布在 hits.eventInfo 中的逻辑。 eventCategory 和 customDimension.value
-
以前帖子中的解决方案存在类似问题,可能会有所帮助。 stackoverflow.com/questions/43880721 和 stackoverflow.com/questions/45231791
标签: sql google-bigquery