forked from dbt-labs/facebook-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
/dbt_modules | ||
/logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
select | ||
* | ||
from | ||
facebook_us.facebook_ads_insights_410871425756349 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
models/stitch/transform/facebook_ad_performance_by_url.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |