Skip to content

Commit

Permalink
athena-compatible macros
Browse files Browse the repository at this point in the history
  • Loading branch information
lukealexmiller committed Aug 31, 2021
1 parent e0c02e1 commit 7917ae5
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
28 changes: 28 additions & 0 deletions macros/stitch/stitch_adwords_click_performance.sql
Original file line number Diff line number Diff line change
@@ -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 %}
71 changes: 71 additions & 0 deletions macros/stitch/stitch_adwords_criteria_performance.sql
Original file line number Diff line number Diff line change
@@ -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 %}
79 changes: 79 additions & 0 deletions macros/stitch/stitch_adwords_url_performance.sql
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 7917ae5

Please sign in to comment.