如何在Google Big Query上添加页面视图维度?

时间:2018-04-18 07:18:09

标签: sql google-analytics google-bigquery

我试图在每个平台的Google Big Query上添加页面视图Dimension(total_userstotal_sessions除外),如您所见。有人知道吗?

SELECT
platform,
SUM(users) AS total_users,
 SUM(sessions) AS total_sessions,
 FROM (
 SELECT
  CASE
      WHEN REGEXP_MATCH(hits.appInfo.id,r'^example$') AND 
 hits.page.hostname IS NULL THEN "Android"
  WHEN REGEXP_MATCH(hits.appInfo.id,r'^com.example.app$')
  AND hits.page.hostname IS NULL THEN "iOS"
  WHEN REGEXP_MATCH(hits.page.hostname,r'^m\..*?example\.') THEN "Mobile"
  WHEN REGEXP_MATCH(hits.page.hostname,r'^\w{2,3}\..*?example\.') THEN 
 "Desktop"
   ELSE "unknown"
 END AS platform,
 count (DISTINCT fullVisitorId) AS users,
 fullvisitorid,
 COUNT(DISTINCT visitid) AS sessions,
 SUM(totals.screenviews) AS screenviews
 FROM
 TABLE_DATE_RANGE([example_table],TIMESTAMP('2018-01-01'),TIMESTAMP('2018-03- 
  31'))
  GROUP EACH BY
  fullvisitorid,
  platform,
  )
  GROUP BY
   platform

1 个答案:

答案 0 :(得分:0)

在您的情况下,您实际上不需要子查询。 其他一些说明:

  • COUNT(DISTINCT)只是遗留SQL中的估算
  • COUNT(DISTINCT visitId)没有任何有用的信息 - 请参阅my answer to this post
  • 对于综合浏览量,我只添加了totals.pageviews的总和。您还可以计算hits.type='PAGE'的点击次数,请参阅Export-Schema
SELECT
  CASE
    WHEN REGEXP_MATCH(hits.appInfo.id,r'^example$') AND hits.page.hostname IS NULL THEN "Android"
    WHEN REGEXP_MATCH(hits.appInfo.id,r'^com.example.app$') AND hits.page.hostname IS NULL THEN "iOS"
    WHEN REGEXP_MATCH(hits.page.hostname,r'^m\..*?example\.') THEN "Mobile"
    WHEN REGEXP_MATCH(hits.page.hostname,r'^\w{2,3}\..*?example\.') THEN "Desktop"
    ELSE "unknown"
  END AS platform,
  COUNT(DISTINCT fullVisitorId) AS usersEst, -- approximate value .. count(distinct) in legacy sql is only an estimation. In standard sql it would be the real deal.
  EXACT_COUNT_DISTINCT(fullVisitorId) as exactUsers,
  -- COUNT(DISTINCT visitid) AS sessions, -- that's wrong! visitIds are only unique per fullvisitorid
  SUM(totals.visits) as sessions,
  SUM(totals.screenviews) AS screenviews,
  SUM(totals.pageviews) AS pageviews
FROM
  TABLE_DATE_RANGE([example_table],TIMESTAMP('2018-01-01'),TIMESTAMP('2018-03-31'))
GROUP BY
  1

请考虑使用标准SQL - 它功能更强大,您可以获得更多控制权。