BigQuery中具有会话ID的Google Analytics(分析)交易ID

时间:2018-06-20 14:40:44

标签: google-bigquery

我正在使用BigQuery中的Google Analytics(分析)数据,并尝试将会话ID与交易ID结合使用。我面临一些困难,因为我尚不了解如何结合带有交易ID(totals.visits = 1)的会话ID(以与我在WHERE子句中使用UNNEST(hits)的GA UI进行匹配)。条件:数据应与GA UI匹配。

所需结果

日期|会话ID |交易编号
2019-06-18 00:00:00.000 UTC | 400101010101 | AAAAAAA
2019-06-18 00:00:00.000 UTC | 400202010102 | BBBBBBB
 2019-06-18 00:00:00.000 UTC | 400303010103 | CCCCCC

当前查询以获取时间戳和SessionIds

#standardSQL
SELECT
TIMESTAMP(PARSE_DATE('%Y%m%d', date)) AS Date,
CONCAT(fullVisitorId, CAST(visitId AS STRING) ) AS SessionID 
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitId AS STRING) )) AS Sessions, /* verify match with GA UI*/
FROM
`ga-xxxxx.yyyyyyyy_sessions_20*`
WHERE
_TABLE_SUFFIX = '180618'
AND totals.visits = 1
GROUP BY
1,2

当前查询已使用TransactionID

使用hits.transaction.transactionIdUNNEST(hits)扩展查询时,与GA UI相比,我得到的记录更多。似乎查询正在计算SessionID和TransactionID。

#standardSQL
SELECT
TIMESTAMP(PARSE_DATE('%Y%m%d', date)) AS Date,
CONCAT(fullVisitorId, CAST(visitId AS STRING) ) AS SessionID, 
hits.transaction.transactionId AS transactionId, 
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitId AS STRING) )) AS Sessions, /* verify match with GA UI*/
FROM
`ga-xxxxx.yyyyyyyy_sessions_20*`,                                  
 UNNEST(hits) AS hits
WHERE
_TABLE_SUFFIX = '180618'
AND totals.visits = 1
GROUP BY
1,2,3

在数据与GA UI匹配的情况下,如何使SessionIDhits.transaction.transactionId进入查询?

希望有人可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:2)

要将GA UI中的会话数与BigQuery匹配起来,您可以像这样对会话进行计数:

CONCAT(CAST(fullVisitorId AS STRING),CAST(visitId AS STRING)) AS sessions

然后,如果要查看会话ID和交易ID,可以执行以下操作:

    SELECT Date,
    CONCAT(CAST(fullVisitorId AS STRING),CAST(visitId AS STRING)) AS visit_ref,
    hits.transaction.transactionID AS transaction_id
    FROM `xxx.xxx.ga_sessions_20180619`
      CROSS JOIN UNNEST(hits) AS hits
    WHERE hits.transaction.transactionID IS NOT NULL
    GROUP BY Date, visit_ref, transaction_id

请注意,我删除了上面未进行任何处理的所有会话,但是如果需要,可以删除此WHERE子句。

如果使用此方法获取更多行,则考虑到用户可能会在多个会话中下达多个订单,您可以使用STRING_AGG在交易代码旁边列出所有交易ID,从而对此有所了解。会话ID,在下面的示例中,我也对它们进行了计数并按此计数进行了排序:

   SELECT Date,
   CONCAT(CAST(fullVisitorId AS STRING),CAST(visitId AS STRING)) AS visit_ref,
   STRING_AGG(hits.transaction.transactionID) AS transaction_id, 
   COUNT(DISTINCT hits.transaction.transactionId) AS orders
   FROM `xxx.xxx.ga_sessions_20180619`
     CROSS JOIN UNNEST(hits) AS hits
   WHERE hits.transaction.transactionID IS NOT NULL
   GROUP BY Date, visit_ref
   ORDER BY orders DESC