2
votes

I'm relatively new to the concept of nested data and trying to get my head around the right way to flatten some GA data in BigQuery (https://support.google.com/analytics/answer/3437719?hl=en).

Now to give some context, for each visitor session I'm trying to capture the list of product SKUs that were looked at (detailed view) and if there was a transaction, the transaction id. By my reckoning, and after doing a bit of research, the simplest way to do this looks like this, using LEFT JOINS to bring back everything:

SELECT fullVisitorId as uId, visitId as vId, h.transaction.transactionId as 
trId, STRING_AGG(p.productSKU, "|") as skus
FROM
`test-bigquery.12345678.ga_sessions_*` t
  LEFT JOIN UNNEST(hits) h
  LEFT JOIN UNNEST(h.product) p
WHERE 
_TABLE_SUFFIX = '20170709'
AND h.eCommerceAction.action_type = '2'
GROUP BY uId, vId, trId

However, this appears to return zero results where trId is not null....

I then had a go at separating the above into two queries and joining. This seems to work and returns a seemingly sensible number of rows (~1000) where trId is not null.

WITH skus AS
(SELECT fullVisitorId as uId, visitId as vId, STRING_AGG(p.productSKU, "|") as skus
    FROM
    `test-bigquery.12345678.ga_sessions_*` t
      LEFT JOIN UNNEST(hits) h
      LEFT JOIN UNNEST(h.product) p
    WHERE 
    _TABLE_SUFFIX = '20170709'
    AND h.eCommerceAction.action_type = '2'
    GROUP BY uId, vId),
transactions AS
  (SELECT fullVisitorId as uId_trans, visitId as vId_trans,  h.transaction.transactionId as trId
    FROM
    `test-bigquery.12345678.ga_sessions_*` t
      LEFT JOIN UNNEST(hits) h
    WHERE 
      _TABLE_SUFFIX = '20170709'
      AND h.transaction.transactionId IS NOT NULL
      GROUP BY uId_trans, vId_trans, trId)
SELECT skus.uId, skus.vId, transactions.trId, skus.skus
FROM skus
LEFT JOIN transactions ON transactions.vId_trans = skus.vId AND transactions.uId_trans = skus.uId

It would be awesome if someone could explain why the two don't give the same answer and hopefully equip me to get involved with all manner of nested fun in the future.... Thanks!

1

1 Answers

2
votes

The mistake you made is happening in this line:

AND h.eCommerceAction.action_type = '2'

If you check ga sessions schema for the field action_type, you'll see that its value is '2' when the customer is just seeing the product and it's '6' when the transaction happened. So, if you filter out only actions equal to '2', you won't be able to get the transactions given that their value is '6'.

In your second query, notice that your query for transactions is no longer filtering out where action is '2' but rather where trasactionId is not null so you are successfully retrieving those rows now.

There are still ways in which you could greatly optimize your query, for instance:

SELECT 
  fullvisitorid,
  visitid,
  ARRAY(SELECT STRUCT(prods.productsku AS sku, MAX(IF(hits.ecommerceaction.action_type = '6', hits.transaction.transactionID, NULL)) AS transactionID) FROM UNNEST(hits) hits, UNNEST(hits.product) prods WHERE hits.ecommerceaction.action_type IN ('2', '6') GROUP BY prods.productsku) result
FROM `test-bigquery.12345678.ga_sessions_*`
WHERE TRUE
  AND _TABLE_SUFFIX = '20170709'
  AND EXISTS(SELECT 1 FROM UNNEST(hits) hits WHERE hits.ecommerceaction.action_type IN ('2', '6'))
LIMIT 1000

This query yields the same results as yours does but it's more succinct and have greater performance (it avoids the unnecessary JOINs and UNNESTs operations and makes good use of the ARRAYs and STRUCTs structure in the data).

I highly recommend learning how to use these techniques with nested data as you'll be able to query hundreds of gigas in a matter of seconds. These are the results I got in my dataset:

enter image description here

It processed more than 500Gbs in 15s.