如何使用Bigquery查找上一页

时间:2019-01-30 15:44:56

标签: google-bigquery

我想找到上一页,当前页是产品页。 例如,我有此页面“ https://www.emag.ro/telefon-mobil-apple-iphone-x-64gb-4g-space-grey-mqac2rm-a/pd/DN094NBBM”,而我的前一页是此页面“ https://www.emag.ro/search/telefoane-mobile/IPHONE/c?ref=srcql” 如何根据点击数返回多少用户有此行为。 我尝试使用此2查询,我想做一个JOIN,但我不知道如何更好。 另外,我尝试了LAG功能,但我不确定是否能抓住所有用户。

谢谢。

with
view_product as (

SELECT 
            ga.fullVisitorId AS GA_USER_ID,
            date as date,
            h.hitnumber as hitnumber,
            CONCAT(ga.fullVisitorId, cast(ga.visitId AS string)) AS SessionID,
            (SELECT VALUE FROM h.customDimensions WHERE INDEX = 10) AS PAGETYPE,
            (SELECT VALUE FROM h.customDimensions WHERE index =8) as ref_parameter,
            visitid as visitid,
            h.page.pagePath as page_path

            FROM 
            `emagbigquery.0` ga, 
            UNNEST(hits) AS h

             WHERE h.type='PAGE'
             AND  _TABLE_SUFFIX = '20190115'
             AND  (SELECT VALUE FROM h.customDimensions WHERE INDEX = 10) = 'viewproduct'

    )         

     ,

     SEARCH_page_WITH_REF_SRCQL as (

    select 
            date as date,
            ga.fullVisitorId AS GA_USER_ID,
            h.hitnumber as hitnumber,
            CONCAT(ga.fullVisitorId, cast(ga.visitId AS string)) AS SessionID,
            (SELECT VALUE FROM h.customDimensions WHERE INDEX = 10) AS PAGETYPE,
            (SELECT VALUE FROM h.customDimensions WHERE index =8) as ref_parameter,
            visitid as visitid,
            h.page.pagePath as page_path

            FROM 
            `emagbigquery.0` ga, 
            UNNEST(hits) AS h

             WHERE h.type='PAGE'
             AND  _TABLE_SUFFIX = '20190115'
             AND  (SELECT VALUE FROM h.customDimensions WHERE INDEX = 10) = 'search'
             AND (SELECT VALUE FROM h.customDimensions WHERE index =8) LIKE 'srcql'



     )

           select 

               COUNT(DISTINCT GA_USER_ID) AS USERS,
               COUNT(DISTINCT SessionID) AS SESSIONS,
              previous_page_from_srcql

               from ( 

             select 
             t1.ga_user_id,
             t1.sessionid,
             t2.hitnumber > t1.hitnumber  as previous_page_from_srcql
              from SEARCH_page_WITH_REF_SRCQL as t1
              inner join view_product as t2
              on t1.ga_user_id = t2.ga_user_id

              group by 
              previous_page_from_srcql

1 个答案:

答案 0 :(得分:0)

尝试UNNEST WITH OFFSET。它可以为您提供一种简便的方法,以便以后确定一行排在另一排之后:

WITH path_and_prev AS (
  SELECT ARRAY(
    SELECT AS STRUCT session.page.pagePath
      , LAG(session.page.pagePath) OVER(ORDER BY i) prevPagePath
    FROM UNNEST(hits) session WITH OFFSET i
  ) x
  FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910`
)

SELECT COUNT(*) c, pagePath, prevPagePath
FROM path_and_prev, UNNEST(x) 
WHERE pagePath='/vests/yellow.html' 
AND prevPagePath='/vests/'
GROUP BY 2,3

enter image description here