我目前正在尝试将 UA 的高级分段重新创建到 bigquery 中,但在获取数字匹配方面遇到问题。
我需要排除曾经完成过“预订”类别活动的用户的所有会话以及来自美国境外的所有会话。我当前的查询使我接近我们的 Looker 仪表板中的数字,但它比应有的要少。
这是我当前的代码:
WITH Excluded_Users AS ( SELECT visitId AS exclusion_id FROM my_data.ga_sessions_*, UNNEST(hits) AS hits WHERE hits.eventInfo.eventCategory = 'Booking' )
SELECT CONCAT( FORMAT("%04d", EXTRACT(YEAR FROM PARSE_DATE("%Y%m%d", date))), '-', FORMAT("%02d", EXTRACT(MONTH FROM PARSE_DATE("%Y%m%d", date))) ) AS year_month,COUNT(DISTINCT IF(totals.visits=1, CONCAT(fullVisitorId, CAST(visitId AS STRING)), NULL) ) as sessions,
FROM my_data.ga_sessions_* s
LEFT JOIN Excluded_Users e ON s.visitId = e.exclusion_id
WHERE e.exclusion_id IS NULL AND s.totals.visits = 1 AND geoNetwork.Country = 'United States'
GROUP BY 1
有几件事:
visitId
只是会话的时间戳(与 visitStartTime
不同,忽略午夜分割)fullvisitorid
+visitstarttime
来计算会话hits
如有必要,通过使用子查询节省一些资源SELECT
CONCAT( FORMAT("%04d", EXTRACT(YEAR FROM PARSE_DATE("%Y%m%d", date))), '-', FORMAT("%02d", EXTRACT(MONTH FROM PARSE_DATE("%Y%m%d", date))) ) AS year_month,
COUNT(DISTINCT fullVisitorId||visitStartTime) as sessions,
FROM `my_data.ga_sessions_*` s
WHERE
NOT exists(select 1 from unnest(hits) where hits.eventInfo.eventCategory = 'Booking')
AND s.totals.visits = 1
AND geoNetwork.Country = 'United States'
GROUP BY 1