diff --git a/.gitignore b/.gitignore index 53676d1..e859da0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ dbcredentials.txt env *.pyc +config.json diff --git a/config.json b/config.json deleted file mode 100644 index 2042e57..0000000 --- a/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "models" : ["pardot", "segment", "snowplow", "trello"], - "schema" : "analyst_collective" -} diff --git a/models/adwords/model.sql b/models/adwords/model.sql new file mode 100644 index 0000000..5e771a0 --- /dev/null +++ b/models/adwords/model.sql @@ -0,0 +1,26 @@ +create or replace view {schema}.adwords_summary as ( + with ad_data as + ( + select + lower(addestinationurl) as cleanurl, + * + from + adwords.adwords12218869_v2 -- How do I make this work regardless of the extension? + ) + select + REPLACE(REGEXP_SUBSTR(cleanurl,'utm_source=[^&]*'),'utm_source=','') as "@utm_source", + REPLACE(REGEXP_SUBSTR(cleanurl,'utm_medium=[^&]*'),'utm_medium=','') as "@utm_medium", + REPLACE(REGEXP_SUBSTR(cleanurl,'utm_campaign=[^&]*'),'utm_campaign=','') as "@utm_campaign", + REPLACE(REGEXP_SUBSTR(cleanurl,'utm_content=[^&]*'),'utm_content=','') as "@utm_content", + REPLACE(REGEXP_SUBSTR(cleanurl,'utm_term=[^&]*'),'utm_term=','') as "@utm_term", + campaign as "@campaign_name" + impressions::integer as "@impressions", + adcost::float as "@cost", + date::date as "@date", + adclicks::integer as "@clicks", + SPLIT_PART(cleanurl,'?',1) as "@base_url", + SPLIT_PART(cleanurl,'?',2) as querystring, + * + from + ad_data + ) diff --git a/models/adwords/model_tests.sql b/models/adwords/model_tests.sql new file mode 100644 index 0000000..145a376 --- /dev/null +++ b/models/adwords/model_tests.sql @@ -0,0 +1,11 @@ +create or replace view {schema}.model_tests + (name, description, result) + as ( + + select + 'adwords_fresher_than_one_day', + 'Most recent adwords entry is no more than one day old', + max("@date"::date) > current_date - '1 day'::interval + from {schema}.adwords_summary + + ); diff --git a/models/facebook_ads/model.sql b/models/facebook_ads/model.sql new file mode 100644 index 0000000..a389f11 --- /dev/null +++ b/models/facebook_ads/model.sql @@ -0,0 +1,35 @@ +create or replace view {schema}.facebook_ads_summary as ( + with ad_data as + ( + select + ag.name as adgroup_name, + lower(nvl(object_url,link_url,object_story_spec__link_data__link)) as addestinationurl, + i.* + from + facebook.facebook_insights_101441173373823 i + join + facebook.facebook_adgroup_101441173373823 ag + on + ag.id = i.adgroup_id + join + facebook.facebook_adcreative_101441173373823 ac + on + ag.creative__id = ac.id + ) + select + REPLACE(REGEXP_SUBSTR(addestinationurl,'utm_source=[^&]*'),'utm_source=','') as "@utm_source", + REPLACE(REGEXP_SUBSTR(addestinationurl,'utm_medium=[^&]*'),'utm_medium=','') as "@utm_medium", + REPLACE(REGEXP_SUBSTR(addestinationurl,'utm_campaign=[^&]*'),'utm_campaign=','') as "@utm_campaign", + REPLACE(REGEXP_SUBSTR(addestinationurl,'utm_content=[^&]*'),'utm_content=','') as "@utm_content", + REPLACE(REGEXP_SUBSTR(addestinationurl,'utm_term=[^&]*'),'utm_term=','') as "@utm_term", + adgroup_name as "@campaign_name", + impressions::integer as "@impressions", + spend::float as "@cost", + date_start::date as "@date", + clicks::integer as "@clicks", + SPLIT_PART(addestinationurl,'?',1) as "@base_url", + SPLIT_PART(addestinationurl,'?',2) as querystring, + * + from + ad_data + ) diff --git a/models/facebook_ads/model_tests.sql b/models/facebook_ads/model_tests.sql new file mode 100644 index 0000000..00dcdb4 --- /dev/null +++ b/models/facebook_ads/model_tests.sql @@ -0,0 +1,11 @@ +create or replace view {schema}.model_tests + (name, description, result) + as ( + + select + 'facebook_ads_fresher_than_one_day', + 'Most recent facebook ads entry is no more than one day old', + max("@date"::date) > current_date - '1 day'::interval + from {schema}.facebook_ads_summary + + ); diff --git a/models/ppc/model.sql b/models/ppc/model.sql new file mode 100644 index 0000000..1af016e --- /dev/null +++ b/models/ppc/model.sql @@ -0,0 +1,51 @@ +-- assuming there is google adwords and facebook ads. +-- need to make this flexible such that we can detect any relevant paid ad platforms and union them in. + +create or replace view {schema}.ppc_consolidated_summary as ( + ( + select + * + from + ( + select + 'Google' as "@adnetwork", + "@campaign_name", + "@utm_source", + "@utm_medium", + "@utm_campaign", + "@utm_content", + "@utm_term", + "@impressions"::integer, + "@cost"::float, + "@date"::date, + "@clicks"::integer, + "@base_url" + from + {schema}.adwords_summary + ) + ) + -- union if other adnetworks present. can we make this happen automatically? + union all + ( + select + * + from + ( + select + 'Facebook' as "@adnetwork", + "@campaign_name", + "@utm_source", + "@utm_medium", + "@utm_campaign", + "@utm_content", + "@utm_term", + "@impressions"::integer, + "@cost"::float, + "@date"::date, + "@clicks"::integer, + "@base_url" + from + {schema}.facebook_summary + ) + ) +)