-
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.
Rescue additional attribution data from BigQuery and refactor to use it.
- Loading branch information
1 parent
0f4e547
commit 5ac23a4
Showing
45 changed files
with
2,269 additions
and
1,728 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% macro get_prefixed_columns(relation_ref, prefix, exclude=[]) %} | ||
|
||
{%- set column_names = dbt_utils.get_filtered_columns_in_relation( | ||
from=relation_ref, | ||
except=['shop_subdomain', 'user_pseudo_id'] + exclude | ||
) | ||
-%} | ||
|
||
{%- for column_name in column_names -%} | ||
{%- if column_name|lower == 'event_timestamp_pt' or column_name|lower == 'created_at' -%} | ||
{{ column_name }} AS {{ prefix }}_at_pt | ||
{%- else -%} | ||
iff( | ||
{{ column_name }}::varchar = '', null, {{ column_name }} | ||
) AS {{ prefix }}_{{ column_name|lower }} | ||
{%- endif -%} | ||
{%- if not loop.last %},{% endif %} | ||
{% endfor -%} | ||
|
||
{% endmacro %} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% macro should_exclude_column_by_prefix(column_name, excluded_prefixes, result) %} | ||
{% for prefix in excluded_prefixes %} | ||
{% if column_name.startswith(prefix) %} | ||
{# {{ log('Debug: excluding column ' ~ column_name ~ ' because it starts with ' ~ prefix, info=True) }} #} | ||
{% set _ = result.append(true) %} | ||
{% break %} | ||
{% endif %} | ||
{% endfor %} | ||
{% endmacro %} |
12 changes: 12 additions & 0 deletions
12
models/google_analytics/intermediate/_int_app_store_attribution_schema.yml
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: 2 | ||
models: | ||
- name: int_app_store_attribution | ||
tests: | ||
- dbt_expectations.expect_table_row_count_to_be_between: | ||
min_value: 200 | ||
columns: | ||
- name: shop_subdomain | ||
tests: | ||
- unique | ||
- not_null | ||
description: The foreign key for the Shop. |
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
6 changes: 1 addition & 5 deletions
6
...staging/_stg_ga_install_events_schema.yml → ...mediate/_int_ga_install_events_schema.yml
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
123 changes: 123 additions & 0 deletions
123
models/google_analytics/intermediate/int_app_store_attribution.sql
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
WITH | ||
shops AS ( | ||
SELECT | ||
shop_subdomain, | ||
first_installed_at_pt | ||
FROM {{ ref('stg_shops') }} | ||
), | ||
|
||
app_store_install_events as ( | ||
select | ||
shop_subdomain, | ||
{{ get_prefixed_columns(ref("int_ga_app_store_page_events"), 'app_store_install', exclude=['event_name']) }} | ||
from {{ ref("int_ga_app_store_page_events") }} | ||
where event_name = 'shopify_app_install' | ||
qualify | ||
row_number() over ( | ||
partition by shop_subdomain order by event_timestamp_pt asc | ||
) | ||
= 1 | ||
), | ||
|
||
app_store_add_app_button_events as ( | ||
select | ||
shop_subdomain, | ||
{{ get_prefixed_columns(ref("int_ga_app_store_page_events"), 'app_store_add_app', exclude=['event_name']) }} | ||
from {{ ref("int_ga_app_store_page_events") }} | ||
where event_name = 'Add App button' | ||
qualify | ||
row_number() over ( | ||
partition by shop_subdomain order by event_timestamp_pt asc | ||
) | ||
= 1 | ||
), | ||
|
||
combined_app_store_install_events AS ( | ||
{%- set column_names = dbt_utils.get_filtered_columns_in_relation( | ||
from=ref('int_ga4_events'), | ||
except=['event_timestamp_pt', 'shop_subdomain', 'user_pseudo_id', 'event_name'] | ||
) | ||
%} | ||
SELECT | ||
shop_subdomain, | ||
{%- for column_name in column_names %} | ||
COALESCE(app_store_install_{{ column_name }}, app_store_add_app_{{ column_name }}) as app_store_install_{{ column_name }} | ||
{%- if not loop.last %},{% endif %} | ||
{%- endfor %}, | ||
COALESCE(app_store_install_at_pt, app_store_add_app_at_pt) as app_store_install_at_pt | ||
FROM app_store_add_app_button_events | ||
INNER JOIN app_store_install_events USING (shop_subdomain) | ||
), | ||
|
||
app_store_ad_clicks as ( | ||
select | ||
shop_subdomain, | ||
{{ get_prefixed_columns(ref("int_ga_app_store_page_events"), 'app_store_ad_click', exclude=['event_name']) }} | ||
from {{ ref("int_ga_app_store_page_events") }} | ||
where page_location ilike '%search_ad%' or event_name = 'shopify_ad_click' | ||
qualify | ||
row_number() over ( | ||
partition by shop_subdomain order by event_timestamp_pt asc | ||
) | ||
= 1 | ||
), | ||
|
||
app_store_ad_click_counts as ( | ||
select | ||
shop_subdomain, | ||
count_if( | ||
app_store_ad_click_at_pt | ||
<= first_installed_at_pt + interval '60min' | ||
) | ||
> 0 as app_store_did_click_ad_before_install, | ||
count(app_store_ad_clicks.app_store_ad_click_page_location) | ||
> 0 as app_store_did_click_ad | ||
from shops | ||
left join app_store_ad_clicks using (shop_subdomain) | ||
group by 1 | ||
), | ||
|
||
app_store_organic_clicks as ( | ||
select | ||
shop_subdomain, | ||
{{ get_prefixed_columns(ref("int_ga_app_store_page_events"), 'app_store_organic_click', exclude=['event_name']) }} | ||
from {{ ref("int_ga_app_store_page_events") }} | ||
where | ||
page_location ilike '%surface_type=%' | ||
and page_location not ilike '%search_ad%' | ||
and event_name = 'session_start' | ||
qualify | ||
row_number() over ( | ||
partition by shop_subdomain order by event_timestamp_pt asc | ||
) | ||
= 1 | ||
), | ||
|
||
final AS ( | ||
SELECT * EXCLUDE (first_installed_at_pt), | ||
coalesce( | ||
app_store_ad_click_app_store_surface_type is not NULL, FALSE | ||
) as app_store_has_ad_click, | ||
coalesce( | ||
app_store_organic_click_app_store_surface_type is not NULL, FALSE | ||
) as has_app_store_organic_click, | ||
|
||
case | ||
when | ||
app_store_has_ad_click = TRUE and has_app_store_organic_click = TRUE | ||
then 'app_store_ad_click_and_organic_click' | ||
when app_store_has_ad_click = TRUE | ||
then 'app_store_ad_click' | ||
when has_app_store_organic_click = TRUE | ||
then 'app_store_organic_click' | ||
else '(direct or predates tracking)' | ||
end as app_store_click_type | ||
FROM shops | ||
LEFT JOIN combined_app_store_install_events USING (shop_subdomain) | ||
LEFT JOIN app_store_ad_clicks USING (shop_subdomain) | ||
LEFT JOIN app_store_organic_clicks USING (shop_subdomain) | ||
LEFT JOIN app_store_ad_click_counts USING (shop_subdomain) | ||
) | ||
|
||
SELECT * | ||
FROM final |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
WITH | ||
user_matching AS (SELECT * FROM {{ ref("stg_anonymous_to_known_user_matching") }}), | ||
|
||
staged_ga4_events AS ( | ||
SELECT * | ||
FROM {{ ref("stg_ga4_events") }} | ||
), | ||
|
||
final AS ( | ||
|
||
SELECT | ||
user_matching.shop_subdomain, | ||
staged_ga4_events.* | ||
EXCLUDE (shopify_id, shop_subdomain) | ||
|
||
FROM staged_ga4_events | ||
LEFT JOIN user_matching | ||
WHERE | ||
staged_ga4_events.user_pseudo_id = user_matching.user_pseudo_id | ||
OR | ||
staged_ga4_events.shopify_id::STRING = user_matching.shopify_id::STRING | ||
OR | ||
staged_ga4_events.shop_subdomain = user_matching.shop_subdomain | ||
) | ||
|
||
SELECT * | ||
FROM final |
14 changes: 14 additions & 0 deletions
14
models/google_analytics/intermediate/int_ga_app_store_page_events.sql
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
with | ||
|
||
source AS ( | ||
SELECT * | ||
FROM {{ ref("int_ga4_events") }} | ||
WHERE | ||
page_location ilike '%apps.shopify.com%' | ||
OR event_name ilike 'shopify%' | ||
OR page_location ilike '%surface_%' | ||
|
||
) | ||
|
||
SELECT * | ||
FROM source |
2 changes: 1 addition & 1 deletion
2
models/mesa/stg_ga_app_store_page_events.yml → ...rmediate/int_ga_app_store_page_events.yml
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
Oops, something went wrong.