不必要的匹配和不受会话限制的自定义维度BigQuery代码过滤器

时间:2019-06-12 11:34:00

标签: google-analytics google-bigquery

我正在尝试根据具有某些自定义维度值的用户过滤渠道。可悲的是,有问题的自定义维度是基于会话范围的,而不是基于匹配的,因此我无法在此特定查询中使用hits.customDimensions。做到这一点并达到预期效果的最佳方法是什么? 找到我到目前为止的进展:


    #standardSQL
    SELECT 
       SUM((SELECT 1 FROM UNNEST(hits) WHERE page.pagePath = '/one - Page' LIMIT 1)) One_Page,
       SUM((SELECT 1 FROM UNNEST(hits) WHERE EXISTS(SELECT 1 FROM UNNEST(hits) WHERE page.pagePath = '/one - Page') AND page.pagePath = '/two - Page' LIMIT 1)) Two_Page,
       SUM((SELECT 1 FROM UNNEST(hits) WHERE EXISTS(SELECT 1 FROM UNNEST(hits) WHERE page.pagePath = '/one - Page') AND page.pagePath = '/three - Page' LIMIT 1)) Three_Page,
       SUM((SELECT 1 FROM UNNEST(hits) WHERE EXISTS(SELECT 1 FROM UNNEST(hits) WHERE page.pagePath = '/one - Page') AND page.pagePath = '/four - Page' LIMIT 1)) Four_Page
    FROM `xxxxxxx.ga_sessions_*`,
    UNNEST(hits) AS h,
    UNNEST(customDimensions) AS cusDim
    WHERE
      _TABLE_SUFFIX BETWEEN '20190320' AND '20190323'
      AND h.hitNumber = 1
      AND cusDim.index = 6
      AND cusDim.value IN ('60','70)

1 个答案:

答案 0 :(得分:0)

具有自定义尺寸的细分

您可以根据自定义维度中的条件过滤会话。只需编写一个子查询来计算感兴趣的案例并将其设置为“> 0”即可。示例数据示例:

SELECT
  fullvisitorid,
  visitstarttime,
  customdimensions
FROM
  `bigquery-public-data.google_analytics_sample.ga_sessions_20170505` t
WHERE
  -- there should be at least one case with index=4 and value='EMEA' ... you can use your index and desired value
  -- unnest() turns customdimensions into table format, so we can apply SQL to this array
  (select count(1)>0 from unnest(customdimensions) where index=4 and value='EMEA')
limit 100

您评论WHERE语句以查看所有数据。

渠道

首先,您可能想了解一下hits数组中发生的事情:

SELECT
  fullvisitorid,
  visitstarttime,
  -- get an overview over relevant hits data
  -- select as struct feeds hits fields into a new array created by array()-function
  ARRAY(select as struct hitnumber, page from unnest(hits) where type='PAGE') hits
FROM
  `bigquery-public-data.google_analytics_sample.ga_sessions_20170505` t
WHERE
  (select count(1)>0 from unnest(customdimensions) where index=4 and value='EMEA')
  and totals.pageviews>3
limit 100

现在,您可以确保数据有意义,您可以创建一个包含相关步骤的点击次数的漏斗数组:

SELECT
  fullvisitorid,
  visitstarttime,
  -- create array with relevant info
  -- cross join hit numbers from step pages to get all combinations so that we can check later which came after the other
  ARRAY(
    select as struct * from
    (select hitnumber as step1 from unnest(hits) where type='PAGE' and page.pagePath='/home') left join
    (select hitnumber as step2 from unnest(hits) where type='PAGE' and page.pagePath like '/google+redesign/%') on true left join
    (select hitnumber as step3 from unnest(hits) where type='PAGE' and page.pagePath='/basket.html') on true
    ) AS funnel
FROM
  `bigquery-public-data.google_analytics_sample.ga_sessions_20170505` t
WHERE
  (select count(1)>0 from unnest(customdimensions) where index=4 and value='EMEA')
  and totals.pageviews>3
limit 100

将其放入WITH语句中以更加清楚,并通过汇总相应的案例来运行分析:

WITH f AS (
  SELECT
    fullvisitorid,
    visitstarttime,
    totals.visits,
    -- create array with relevant info
    -- cross join hit numbers from step pages to get all combinations so that we can check later which came after the other
    ARRAY(
      select as struct * from
        (select hitnumber as step1 from unnest(hits) where type='PAGE' and page.pagePath='/home') left join
        (select hitnumber as step2 from unnest(hits) where type='PAGE' and page.pagePath like '/google+redesign/%') on true left join
        (select hitnumber as step3 from unnest(hits) where type='PAGE' and page.pagePath='/basket.html') on true
      ) AS funnel
  FROM
    `bigquery-public-data.google_analytics_sample.ga_sessions_20170505` t
  WHERE
    (select count(1)>0 from unnest(customdimensions) where index=4 and value='EMEA')
    and totals.pageviews>3
)

SELECT 
  COUNT(DISTINCT fullvisitorid) as users,
  SUM(visits) as allSessions,
  SUM( IF(array_length(funnel)>0,visits,0) ) sessionsWithFunnelPages,
  SUM( IF( (select count(1)>0 from unnest(funnel) where step1 is not null ) ,visits,0) ) sessionsWithStep1,
  SUM( IF( (select count(1)>0 from unnest(funnel) where step1 is not null and step1<step2 ) ,visits,0) ) sessionsFunnelToStep2,
  SUM( IF( (select count(1)>0 from unnest(funnel) where step1 is not null and step1<step2 and step2<step3 and step1<step3) ,visits,0) ) sessionsFunnelToStep3
FROM f

请在使用前进行测试。