【问题标题】:Need BigQuery SQL query to collect time on page from Google Analytics data需要 BigQuery SQL 查询以从 Google Analytics 数据中收集页面停留时间
【发布时间】:2017-02-21 09:54:24
【问题描述】:

任何人都可以帮助使用 BIgQuery SQL 查询从 Google Analytics 数据中提取特定页面的页面停留时间吗?

对于访问过特定页面的每个 visitorId,我想要该页面的页面停留时间。这样我就可以计算页面上的中位时间而不是平均值。

我假设需要 visitorId、hits.hitsNumber 和 hits.time 维度。此外,需要从下一次点击的 hits.time 中减去查看页面的点击的 hits.time。

非常感谢任何帮助。

【问题讨论】:

    标签: google-analytics google-bigquery


    【解决方案1】:

    试试这个:

    SELECT 
      fullVisitorId,
      hits.page.hostname,
      hits.page.pagePath,
      hits.hitNumber,
      hits.time,
      nextTime,
      (nextTime - hits.time) as timeOnPage
    FROM(
      SELECT
        fullVisitorId, 
        hits.page.hostname,
        hits.page.pagePath,
        hits.hitNumber,
        hits.time,
        LEAD(hits.time, 1) OVER (PARTITION BY fullVisitorId, visitNumber ORDER BY hits.time ASC) as nextTime
      FROM [PROJECTID:DATASETID.ga_sessions_YYYYMMDD]
      WHERE hits.type = "PAGE"
    )
    

    这段代码的关键是LEAD() 函数,它根据PARTITION BYORDER BY 限定符从分区中的下一行获取指定值。

    希望有帮助!

    【讨论】:

    • 谢谢你,@andre622。这很有帮助。一个问题:我们不应该添加code where hits.type='PAGE' code 以确保我们计算的是页面之间的时间而不是所有的点击吗?
    • 会话的最后一次综合浏览会发生什么?根据此查询,nextTime 将为 NULL,然后 (NULL - hits.time) 作为 timeOnPage 出来为 NULL?
    • 看起来 GA 不计算会话最后一页的页面停留时间(这被认为是退出) - 所以上面是一个有争议的问题。
    【解决方案2】:

    要考虑最后一页时间,可以使用此查询,它会在最后一页上给出零时间,因为 BQ 没有办法计算在最后一页上花费的时间,但它至少会给出零而不是空。

    SELECT 
      fullVisitorId,
      hits.page.hostname,
      hits.page.pagePath,
      hits.hitNumber,
      hits.time,
      nextTime,
      CASE
        WHEN hits.isExit IS NOT NULL THEN last_interaction - hit_time
        ELSE next_pageview - hit_time
      END
      AS time_on_page
    FROM(
      SELECT
        fullVisitorId, 
        hits.page.hostname,
        hits.page.pagePath,
        hits.hitNumber,
        hits.isExit,
        hits.time/1000 as hits_time,
        LEAD(hits.time/1000, 1) OVER (PARTITION BY fullVisitorId, visitNumber ORDER BY hits.time ASC) as nextTime,
        MAX(IF(hits.isInteraction = TRUE,hits.time / 1000,0)) OVER (PARTITION BY fullVisitorId, visitStartTime) AS last_interaction
      FROM [PROJECTID:DATASETID.ga_sessions_YYYYMMDD]
      WHERE hits.type = "PAGE"
    )
    

    【讨论】:

    • 另请注意,BQ/GA 以毫秒为单位计算时间,因此您需要将其除以 1000,这就是它存在的原因。
    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多