From 7e81b8b774c07742beb78d3583d430d937f37d8e Mon Sep 17 00:00:00 2001 From: Tristan Handy Date: Wed, 24 Aug 2016 18:25:13 -0400 Subject: [PATCH] initial models --- .gitignore | 3 ++ dbt_project.yml | 20 +++++++++ models/stitch/base/facebook_ad_creatives.sql | 10 +++++ models/stitch/base/facebook_ad_insights.sql | 4 ++ models/stitch/base/facebook_ads.sql | 7 +++ models/stitch/base/schema.yml | 18 ++++++++ .../facebook_ad_performance_by_url.sql | 43 +++++++++++++++++++ tests/campaign_unique_for_ad.sql | 26 +++++++++++ 8 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 dbt_project.yml create mode 100644 models/stitch/base/facebook_ad_creatives.sql create mode 100644 models/stitch/base/facebook_ad_insights.sql create mode 100644 models/stitch/base/facebook_ads.sql create mode 100644 models/stitch/base/schema.yml create mode 100644 models/stitch/transform/facebook_ad_performance_by_url.sql create mode 100644 tests/campaign_unique_for_ad.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01f5132 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/dbt_modules +/logs diff --git a/dbt_project.yml b/dbt_project.yml new file mode 100644 index 0000000..28c177f --- /dev/null +++ b/dbt_project.yml @@ -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" diff --git a/models/stitch/base/facebook_ad_creatives.sql b/models/stitch/base/facebook_ad_creatives.sql new file mode 100644 index 0000000..f53eb02 --- /dev/null +++ b/models/stitch/base/facebook_ad_creatives.sql @@ -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 diff --git a/models/stitch/base/facebook_ad_insights.sql b/models/stitch/base/facebook_ad_insights.sql new file mode 100644 index 0000000..d85928d --- /dev/null +++ b/models/stitch/base/facebook_ad_insights.sql @@ -0,0 +1,4 @@ +select + * +from + facebook_us.facebook_ads_insights_410871425756349 diff --git a/models/stitch/base/facebook_ads.sql b/models/stitch/base/facebook_ads.sql new file mode 100644 index 0000000..a19edf2 --- /dev/null +++ b/models/stitch/base/facebook_ads.sql @@ -0,0 +1,7 @@ +select + id, + name, + creative__id as creative_id, + created_time as created_at +from + facebook_us.facebook_ads_410871425756349 diff --git a/models/stitch/base/schema.yml b/models/stitch/base/schema.yml new file mode 100644 index 0000000..926b7c5 --- /dev/null +++ b/models/stitch/base/schema.yml @@ -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 diff --git a/models/stitch/transform/facebook_ad_performance_by_url.sql b/models/stitch/transform/facebook_ad_performance_by_url.sql new file mode 100644 index 0000000..65b655e --- /dev/null +++ b/models/stitch/transform/facebook_ad_performance_by_url.sql @@ -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 diff --git a/tests/campaign_unique_for_ad.sql b/tests/campaign_unique_for_ad.sql new file mode 100644 index 0000000..9422b6a --- /dev/null +++ b/tests/campaign_unique_for_ad.sql @@ -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