From 7917ae57e16eaadba4d74535ca62785202338508 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Tue, 31 Aug 2021 22:09:29 +0200 Subject: [PATCH] athena-compatible macros --- .../stitch_adwords_click_performance.sql | 28 +++++++ .../stitch_adwords_criteria_performance.sql | 71 +++++++++++++++++ .../stitch/stitch_adwords_url_performance.sql | 79 +++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 macros/stitch/stitch_adwords_click_performance.sql create mode 100644 macros/stitch/stitch_adwords_criteria_performance.sql create mode 100644 macros/stitch/stitch_adwords_url_performance.sql diff --git a/macros/stitch/stitch_adwords_click_performance.sql b/macros/stitch/stitch_adwords_click_performance.sql new file mode 100644 index 0000000..7088990 --- /dev/null +++ b/macros/stitch/stitch_adwords_click_performance.sql @@ -0,0 +1,28 @@ +{% macro stitch_adwords_click_performance() %} + + {{ adapter.dispatch('stitch_adwords_click_performance', 'adwords')() }} + +{% endmacro %} + + +{% macro athena__stitch_adwords_click_performance() %} + +with gclid_base as ( + + select + + googleclickid as gclid, + cast(from_iso8601_timestamp(day) as date) as date_day, + keywordid as criteria_id, + adgroupid as ad_group_id, + row_number() over (partition by googleclickid order by cast(from_iso8601_timestamp(day) as date)) as row_num + + from {{ var('click_performance_report') }} + +) + +select * from gclid_base +where row_num = 1 +and gclid is not null + +{% endmacro %} \ No newline at end of file diff --git a/macros/stitch/stitch_adwords_criteria_performance.sql b/macros/stitch/stitch_adwords_criteria_performance.sql new file mode 100644 index 0000000..59a9222 --- /dev/null +++ b/macros/stitch/stitch_adwords_criteria_performance.sql @@ -0,0 +1,71 @@ +{% macro stitch_adwords_criteria_performance() %} + + {{ adapter.dispatch('stitch_adwords_criteria_performance', 'adwords')() }} + +{% endmacro %} + + +{% macro athena__stitch_adwords_criteria_performance() %} + +with criteria_base as ( + + select * from {{ var('criteria_performance_report') }} + +), + +aggregated as ( + + select + + cast( + {{ dbt_utils.surrogate_key ( + [ + 'customerid', + 'keywordid', + 'adgroupid', + 'day' + ] + ) }} as varchar) as id, + + cast(from_iso8601_timestamp(day) as date) as date_day, + keywordid as criteria_id, + adgroup as ad_group_name, + adgroupid as ad_group_id, + adgroupstate as ad_group_state, + campaign as campaign_name, + campaignid as campaign_id, + campaignstate as campaign_state, + customerid as customer_id, + _sdc_report_datetime, + sum(clicks) as clicks, + sum(impressions) as impressions, + sum(cast((cast(cost as double)/cast(1000000 as double)) as decimal(38,6))) as spend + + from criteria_base + {{ dbt_utils.group_by(11) }} + +), + +ranked as ( + + select + + *, + rank() over (partition by id + order by _sdc_report_datetime desc) as latest + + from aggregated + +), + +final as ( + + select * + from ranked + where latest = 1 + +) + +select * from final + +{% endmacro %} \ No newline at end of file diff --git a/macros/stitch/stitch_adwords_url_performance.sql b/macros/stitch/stitch_adwords_url_performance.sql new file mode 100644 index 0000000..6641bb1 --- /dev/null +++ b/macros/stitch/stitch_adwords_url_performance.sql @@ -0,0 +1,79 @@ +{% macro stitch_adwords_url_performance() %} + + {{ adapter.dispatch('stitch_adwords_url_performance', 'adwords')() }} + +{% endmacro %} + + +{% macro athena__stitch_adwords_url_performance() %} + +with url_performance_base as ( + + select * from {{ var('final_url_performance_report') }} + +), + +aggregated as ( + + select + + cast({{ dbt_utils.surrogate_key ( + [ + 'customerid', + 'finalurl', + 'day', + 'campaignid', + 'adgroupid' + ] + ) }} as varchar) as id, + + cast(from_iso8601_timestamp(day) as date) as date_day, + + {{ dbt_utils.split_part('finalurl', "'?'", 1) }} as base_url, + {{ dbt_utils.get_url_host('finalurl') }} as url_host, + '/' || {{ dbt_utils.get_url_path('finalurl') }} as url_path, + {{ dbt_utils.get_url_parameter('finalurl', 'utm_source') }} as utm_source, + {{ dbt_utils.get_url_parameter('finalurl', 'utm_medium') }} as utm_medium, + {{ dbt_utils.get_url_parameter('finalurl', 'utm_campaign') }} as utm_campaign, + {{ dbt_utils.get_url_parameter('finalurl', 'utm_content') }} as utm_content, + {{ dbt_utils.get_url_parameter('finalurl', 'utm_term') }} as utm_term, + campaignid as campaign_id, + campaign as campaign_name, + adgroupid as ad_group_id, + adgroup as ad_group_name, + customerid as customer_id, + _sdc_report_datetime, + + sum(clicks) as clicks, + sum(impressions) as impressions, + sum(cast((cast(cost as double)/cast(1000000 as double)) as decimal(38,6))) as spend + + from url_performance_base + + {{ dbt_utils.group_by(16) }} + +), + +ranked as ( + + select + + *, + rank() over (partition by id + order by _sdc_report_datetime desc) as latest + + from aggregated + +), + +final as ( + + select * + from ranked + where latest = 1 + +) + +select * from final + +{% endmacro %} \ No newline at end of file