Skip to content

Commit

Permalink
initial models
Browse files Browse the repository at this point in the history
  • Loading branch information
jthandy committed Aug 24, 2016
1 parent 5c85320 commit 7e81b8b
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/dbt_modules
/logs
20 changes: 20 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: 'facebook-ads'
version: '1.0'

source-paths: ["models"] # paths with source code to compile
analysis-paths: ["analysis"] # path with analysis files which are compiled, but not run
target-path: "target" # path for compiled code
clean-targets: ["target"] # directories removed by the clean task
test-paths: ["test"] # where to store test results
data-paths: ["data"] # load CSVs from this directory with `dbt seed`

models:
facebook-ads:
stitch:
enabled: true
materialized: view
fivetran:
enabled: false
materialized: view

profile: "casper"
10 changes: 10 additions & 0 deletions models/stitch/base/facebook_ad_creatives.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
select
id,
url_tags,
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
4 changes: 4 additions & 0 deletions models/stitch/base/facebook_ad_insights.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select
*
from
facebook_us.facebook_ads_insights_410871425756349
7 changes: 7 additions & 0 deletions models/stitch/base/facebook_ads.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
id,
name,
creative__id as creative_id,
created_time as created_at
from
facebook_us.facebook_ads_410871425756349
18 changes: 18 additions & 0 deletions models/stitch/base/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
facebook_ad_creatives:
not_null:
- id

facebook_ads:
constraints:
relationships:
- {from: creative_id, to: facebook_ad_creatives, field: id}
not_null:
- creative_id
- id

facebook_ad_insights:
constraints:
relationships:
- {from: ad_id, to: facebook_ads, field: id}
not_null:
- ad_id
43 changes: 43 additions & 0 deletions models/stitch/transform/facebook_ad_performance_by_url.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
with ads 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
*,
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
26 changes: 26 additions & 0 deletions tests/campaign_unique_for_ad.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
with ads as (

select * from dbt_jthandy.facebook_ads

), creatives as (

select * from dbt_jthandy.facebook_ad_creatives

), insights as (

select * from dbt_jthandy.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

)

select
ad_id, count(distinct campaign_id)
from joined
group by 1
having count(distinct campaign_id) > 1

0 comments on commit 7e81b8b

Please sign in to comment.