forked from Velir/dbt-ga4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor page conversion count and unit test (Velir#98)
* refactor page conversion count and test * remove breakpoint
- Loading branch information
1 parent
d18a9cd
commit 759031d
Showing
4 changed files
with
20 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,11 @@ | ||
{{ config( | ||
enabled= var('conversion_events', false) != false | ||
) }} | ||
with events as ( | ||
select | ||
1 as event_count, | ||
event_name, | ||
event_date_dt, | ||
extract( hour from (select timestamp_micros(event_timestamp))) as hour, | ||
page_location | ||
from {{ref('stg_ga4__events')}} | ||
), | ||
pk as ( | ||
select | ||
distinct (concat( cast(event_date_dt as string), cast(hour as string), page_location )) as page_key, | ||
from events | ||
) | ||
-- For loop that creates 1 cte per conversions, grouped by page_location | ||
{% for ce in var('conversion_events',[]) %} | ||
, | ||
conversion_{{ce}} as ( | ||
select | ||
distinct (concat( cast(event_date_dt as string), cast(hour as string), page_location )) as page_key, | ||
sum(event_count) as conversion_count, | ||
from events | ||
where event_name = '{{ce}}' | ||
group by page_key | ||
) | ||
|
||
{% endfor %} | ||
|
||
, | ||
-- Finally, join in each conversion count as a new column | ||
final_pivot as ( | ||
select | ||
page_key | ||
{% for ce in var('conversion_events',[]) %} | ||
, ifnull(conversion_{{ce}}.conversion_count,0) as {{ce}}_count | ||
{% endfor %} | ||
from pk | ||
select | ||
page_key | ||
{% for ce in var('conversion_events',[]) %} | ||
left join conversion_{{ce}} using (page_key) | ||
, countif(event_name = '{{ce}}') as {{ce}}_count | ||
{% endfor %} | ||
) | ||
|
||
select * from final_pivot | ||
|
||
from {{ref('stg_ga4__events')}} | ||
group by 1 |
This file contains 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