Google Analytics(分析)与BigQuery之间的会话不匹配,同时取消匹配hits和hits.product

时间:2019-11-26 00:11:27

标签: google-bigquery unnest

当我尝试运行以下查询时,Google Analytics(分析)和BQ数据之间的数据差异为15%:

SELECT
  SUM(Sessions) AS Sessions
FROM (
  SELECT
    PARSE_DATE("%Y%m%d",
      date) AS DATE,
    COUNT(DISTINCT CONCAT(fullVisitorId,"-",CAST(visitStartTime AS STRING))) AS Sessions,
    (COUNT(DISTINCT
        CASE
          WHEN totals.bounces = 1 THEN CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))
          ELSE NULL
        END ) / COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))))*100 AS Bounce_Rate,
    COUNT(DISTINCT hits.transaction.transactionId) AS Transactions,
    SUM(hits.transaction.transactionRevenue)/1000000 AS Revenue,
    SUM(p.productRevenue)/1000000 AS Product_Revenue,
    (COUNT(DISTINCT hits.transaction.transactionId) / COUNT(DISTINCT CONCAT(CAST(fullVisitorId AS STRING), CAST(visitStartTime AS STRING))))*100 AS Ecommerce_Conversion_Rate,
    (SUM(hits.transaction.transactionRevenue)/1000000)/COUNT(DISTINCT hits.transaction.transactionId) AS Avg_Order_Value,
    SUM(hits.item.itemQuantity) / COUNT(hits.transaction.transactionId) AS Avg_Quantity,
    device.deviceCategory AS DeviceCategory,
    channelGrouping AS DefaultChannelGrouping,
    CONCAT(trafficSource.source," / ",trafficSource.medium) AS Source_Medium
  FROM
    `[Project_ID].[Dataset].ga_sessions_2019*`,
    UNNEST(hits) AS hits,
    UNNEST(hits.product) AS p
  GROUP BY
    DATE,
    DeviceCategory,
    DefaultChannelGrouping,
    Source_Medium )
WHERE
  DATE BETWEEN "2019-11-17"
  AND "2019-11-23"

但是,当我摆脱UNNEST(hits.product) AS p时,我得到的差异就会降低。我想知道如何一起UNNEST hitshits.product数据

1 个答案:

答案 0 :(得分:0)

您正在与产品阵列交叉连接。如果缺少产品数组,则交叉联接将导致NULL-有效擦除整个匹配,有时甚至是整个会话(如果仅包含一个匹配而没有产品信息)。 您必须使用产品数组LEFT JOIN来防止删除匹配/会话。

FROM `[Project_ID].[Dataset].ga_sessions_2019*` AS t
  CROSS JOIN UNNEST(hits) AS h 
  LEFT JOIN  UNNEST(product) AS p

或简而言之

FROM `[Project_ID].[Dataset].ga_sessions_2019*` AS t, t.hits h LEFT JOIN h.product p