如何使用BigQuery创建频道路径?

时间:2017-04-13 14:45:00

标签: google-analytics google-bigquery

我想根据用户访问网站的顺序为每个用户ID创建一个频道路径,并且我想总计每个路径的总事务数。我的想法是用Bigquery做到这一点。

我有以下输入表:

           user id - date       - hits.time - channelgrouping - transaction
           xxxxxxx - 2017-01-01 - 23234     - google cpc      - 1          
           xxxxxxx - 2017-01-02 - 23234     - email           - 0           

我想要的输出表是:

           user id - channelgrouping path - transaction
           xxxxxxx - google cpc > email   - 1

任何人都可以通过提供创建路径的代码来帮助我开始吗?

提前致谢!

1 个答案:

答案 0 :(得分:4)

见下面的例子和方向

  
QGuiApplication

输出如下

#standardSQL
WITH yourTable AS (
  SELECT 1 AS user_id, '2017-01-01' AS DATE, 'google cpc' AS channelgrouping, 1 AS transaction UNION ALL
  SELECT 1, '2017-01-02', 'email', 0 UNION ALL
  SELECT 2, '2017-01-01', 'abc', 2 UNION ALL
  SELECT 2, '2017-01-02', 'xyz', 3 
)
SELECT 
  user_id, 
  STRING_AGG(channelgrouping, ' > ') AS channelgrouping_path,
  SUM(transaction) AS transaction
FROM yourTable
GROUP BY user_id
-- ORDER BY USER_ID  
  

基于您的确切查询的示例:

user_id channelgrouping_path    transaction  
1       google cpc > email      1    
2       abc > xyz               5    

确保将#standardSQL SELECT visitorId ,STRING_AGG(channelgrouping, ' > ') AS channelgrouping_path ,SUM(transactions) AS transaction FROM ( SELECT date ,visitorId ,channelgrouping ,SUM(totals.transactions) AS transactions FROM `project.dataset.table` GROUP BY date ,visitorId ,channelGrouping ) GROUP BY visitorId 替换为您的

  

我将不得不按日期和hits.time订购数据集,这对于执行而言非常繁重。

查看带有聚合字符串

的控制顺序的示例
project.dataset.table