Last active
June 15, 2022 13:05
-
-
Save stanasiukcom/e863ee784414530e66bb6c07033e2919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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