Skip to content

Instantly share code, notes, and snippets.

@ZordnajelA
Last active February 19, 2024 03:30
Show Gist options
  • Save ZordnajelA/e3d7574f68fe67863d13dcccbd3ac765 to your computer and use it in GitHub Desktop.
Save ZordnajelA/e3d7574f68fe67863d13dcccbd3ac765 to your computer and use it in GitHub Desktop.
Using SQL FIRST_VALUE() window function to replicate GA4 traffic source attribution behavior
FIRST_VALUE(
(SELECT
value.string_value
FROM
UNNEST(event_params)
WHERE
key = 'name_of_traffic_source_parameter'
--source, medium, campaign, content, term, campaign_id
)
IGNORE nulls
) OVER (
PARTITION BY
user_pseudo_id,
(SELECT
value.int_value
FROM
UNNEST(event_params)
WHERE
key = 'ga_session_id'
)
ORDER BY
event_timestamp
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS my_utm_parameter_session_level
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment