Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save stanasiukcom/e863ee784414530e66bb6c07033e2919 to your computer and use it in GitHub Desktop.

Select an option

Save stanasiukcom/e863ee784414530e66bb6c07033e2919 to your computer and use it in GitHub Desktop.
WITH unnestedHits AS (
SELECT
fullVisitorId
,visitId
,visitStartTime
,h.*
FROM
`bigquery-public-data.google_analytics_sample.ga_sessions_20170801`
,UNNEST(hits) AS h
)
SELECT
foundEvents.fullVisitorId
,foundEvents.visitId
,uh.visitStartTime
,uh.time
,uh.page.pagePath
,uh.eventInfo.eventCategory
,uh.eventInfo.eventAction
FROM
(
SELECT
fullVisitorId
,visitId
,ARRAY_AGG(hitNumber) OVER(PARTITION BY fullVisitorId, visitId, hitNumber) AS hitNumbers
FROM
(
SELECT
fullVisitorId
,visitId
# ,visitStartTime
,hitNumber
# ,h.page.pagePath
# ,h.eventInfo.eventCategory
# ,h.eventInfo.eventAction
,CASE WHEN eventInfo.eventAction = 'Add to Cart' THEN 'yes' ELSE NULL END AS hasAddedToCart
FROM
unnestedHits
WHERE
eventInfo IS NOT NULL
ORDER BY
1
,2
,3
,4
)
WHERE
hasAddedToCart = 'yes'
) AS foundEvents
INNER JOIN
unnestedHits AS uh
ON
foundEvents.fullVisitorId = uh.fullVisitorId AND foundEvents.visitId = uh.visitId AND foundEvents.hitNumbers[ORDINAL(1)] = uh.hitNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment