Skip to content

Commit

Permalink
Merge pull request #1 from fishtown-analytics/feature/vars
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
jthandy authored Oct 20, 2016
2 parents 7e81b8b + ecad07c commit 5279035
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 46 deletions.
8 changes: 6 additions & 2 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ models:
stitch:
enabled: true
materialized: view
vars:
ad_creatives_table: #schema.table
ads_insights_table: #schema.table
ads_table: #schema.table
campaigns_table: #schema.table
ads_insights_agegender_table: #schema.table
fivetran:
enabled: false
materialized: view

profile: "casper"
22 changes: 18 additions & 4 deletions models/stitch/base/facebook_ad_creatives.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
with base as (

select
id,
url_tags,
coalesce(
object_story_spec__link_data__call_to_action__value__link,
object_story_spec__link_data__link,
link_url
) as url
from
{{ var('ad_creatives_table') }}

)

select
id,
url_tags,
*,
split_part(url ,'?',1) as base_url,
split_part(split_part(url_tags,'utm_source=',2), '&', 1) as utm_source,
split_part(split_part(url_tags,'utm_medium=',2), '&', 1) as utm_medium,
split_part(split_part(url_tags,'utm_campaign=',2), '&', 1) as utm_campaign,
split_part(split_part(url_tags,'utm_content=',2), '&', 1) as utm_content,
split_part(split_part(url_tags,'utm_term=',2), '&', 1) as utm_term
from
facebook_us.facebook_adcreative_410871425756349
from base
2 changes: 1 addition & 1 deletion models/stitch/base/facebook_ad_insights.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
select
*
from
facebook_us.facebook_ads_insights_410871425756349
{{ var('ads_insights_table') }}
2 changes: 1 addition & 1 deletion models/stitch/base/facebook_ads.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ select
creative__id as creative_id,
created_time as created_at
from
facebook_us.facebook_ads_410871425756349
{{ var('ads_table') }}
4 changes: 4 additions & 0 deletions models/stitch/base/facebook_ads_insights_age_gender.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select
*
from
{{ var('ads_insights_agegender_table') }}
5 changes: 5 additions & 0 deletions models/stitch/base/facebook_campaigns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
select
id as campaign_id,
name as campaign_name
from
{{ var('campaigns_table') }}
25 changes: 25 additions & 0 deletions models/stitch/transform/facebook_ad_insights_age_gender_xf.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
with ads as (

select * from {{ref('facebook_ads')}}

), creatives as (

select * from {{ref('facebook_ad_creatives')}}

), insights as (

select * from {{ref('facebook_ads_insights_age_gender')}}

)
select
insights.*,
creatives.base_url,
creatives.url,
creatives.utm_medium,
creatives.utm_source,
creatives.utm_campaign,
creatives.utm_content,
creatives.utm_term
from insights
inner join ads on insights.ad_id = ads.id
inner join creatives on ads.creative_id = creatives.id
25 changes: 25 additions & 0 deletions models/stitch/transform/facebook_ad_insights_xf.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
with ads as (

select * from {{ref('facebook_ads')}}

), creatives as (

select * from {{ref('facebook_ad_creatives')}}

), insights as (

select * from {{ref('facebook_ad_insights')}}

)
select
insights.*,
creatives.base_url,
creatives.url,
creatives.utm_medium,
creatives.utm_source,
creatives.utm_campaign,
creatives.utm_content,
creatives.utm_term
from insights
inner join ads on insights.ad_id = ads.id
inner join creatives on ads.creative_id = creatives.id
55 changes: 17 additions & 38 deletions models/stitch/transform/facebook_ad_performance_by_url.sql
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
with ads as (
with insights as (

select * from {{ref('facebook_ads')}}

), creatives as (

select * from {{ref('facebook_ad_creatives')}}

), insights as (

select * from {{ref('facebook_ad_insights')}}

), joined as (

select
*
from insights
inner join ads on insights.ad_id = ads.id
inner join creatives on ads.creative_id = creatives.id

), aggregated as (

select
date_start::date as insight_date,
utm_medium,
utm_source,
utm_campaign,
utm_content,
utm_term,
sum(impressions) as impressions,
sum(spend) as spend,
sum(website_clicks) as clicks
from joined
group by 1, 2, 3, 4, 5, 6
select * from {{ref('facebook_ad_insights_xf')}}

)

select
*,
spend / nullif(clicks, 0) as cpc,
spend / (nullif(impressions, 0) * .001::numeric(38,6)) as cpm
from aggregated
order by 1, 2, 3, 4, 5, 6
date_start::date as insight_date,
ad_id,
campaign_id,
url,
base_url,
utm_medium,
utm_source,
utm_campaign,
utm_content,
utm_term,
sum(impressions) as impressions,
sum(spend) as spend,
sum(website_clicks) as clicks
from insights
group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
with insights as (

select * from {{ref('facebook_ad_insights_age_gender_xf')}}

)

select
date_start::date as insight_date,
ad_id,
campaign_id,
age,
gender,
url,
base_url,
utm_medium,
utm_source,
utm_campaign,
utm_content,
utm_term,
sum(coalesce(impressions__bigint, nullif(impressions__string, '')::int)) as impressions, --fix!
sum(spend) as spend,
sum(website_clicks) as clicks
from insights
group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

0 comments on commit 5279035

Please sign in to comment.