Skip to content

Commit

Permalink
Merge pull request #22 from fishtown-analytics/fix/ad-creative-url-pa…
Browse files Browse the repository at this point in the history
…rsing

Fix: ad creative url parsing--child links
  • Loading branch information
axelazaid authored May 16, 2019
2 parents 6afeb62 + 868b1be commit 2de1382
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 58 deletions.
5 changes: 4 additions & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ data-paths: ["data"] # load CSVs from this directory with `dbt seed`
models:
facebook_ads:
vars:

etl: #stitch or fivetran
ads_table: #table
ad_creatives_table: #table
adsets_table: #table
campaigns_table: #table
ads_insights_table: #table
url_tag_table: #only for fivetran
ad_creatives__child_links_table: #table -- disable if on snowflake

url_tag_table: #only for fivetran
26 changes: 26 additions & 0 deletions macros/nested_field.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% macro nested_field(field, subfields) %}

{{ adapter_macro('facebook_ads.nested_field', field, subfields) }}

{% endmacro %}


{% macro default__nested_field(field, subfields) %}

{{field}}__{{ subfields|join('__') }}

{% endmacro %}


{% macro bigquery__nested_field(field, subfields) %}

{{field}}.{{ subfields|join('.') }}

{% endmacro %}


{% macro snowflake__nested_field(field, subfields) %}

{{field ~ "['" ~ subfields|join("']['") ~ "']" }}

{% endmacro %}
78 changes: 24 additions & 54 deletions macros/stitch/base/stitch_fb_ad_creatives.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,51 @@
{% macro default__stitch_fb_ad_creatives() %}

with base as (

select

id as creative_id,
lower(nullif(url_tags, '')) as url_tags,
lower(coalesce(
nullif(object_story_spec__link_data__call_to_action__value__link, ''),
nullif(object_story_spec__video_data__call_to_action__value__link, ''),
nullif(object_story_spec__link_data__link, '')
)) as url
select * from {{ var('ad_creatives_table') }}

from
{{ var('ad_creatives_table') }}

), splits as (
),

select
child_links as (

creative_id,
url,
{{ dbt_utils.split_part('url', "'?'", 1) }} as base_url,
--this is a strange thing to have to do but it's because sometimes
--the URL exists on the story object and we wouldn't get the appropriate
--UTM params here otherwise
coalesce(url_tags, {{ dbt_utils.split_part('url', "'?'", 2) }} ) as url_tags
select * from {{ ref('fb_ad_creatives__child_links') }}

from base

)

select
),

*,
{{ dbt_utils.get_url_host('url') }} as url_host,
'/' || {{dbt_utils.get_url_path('url') }} as url_path,

{{ facebook_ads.get_url_parameter() }}
links_joined as (

from splits

{% endmacro %}


{% macro snowflake__stitch_fb_ad_creatives() %}

with base as (

select

id as creative_id,

lower(coalesce(
nullif(object_story_spec['link_data']['call_to_action']['value']['link']::varchar, ''),
nullif(object_story_spec['video_data']['call_to_action']['value']['link']::varchar, ''),
nullif(object_story_spec['link_data']['link']::varchar, '')
)) as url,
nullif(child_link, ''),
nullif({{ facebook_ads.nested_field('base.object_story_spec', ['link_data', 'call_to_action', 'value', 'link']) }}, ''),
nullif({{ facebook_ads.nested_field('base.object_story_spec', ['video_data', 'call_to_action', 'value', 'link']) }}, ''),
nullif({{ facebook_ads.nested_field('base.object_story_spec', ['link_data', 'link']) }}, '')
)) as url,

url_tags

from {{ var('ad_creatives_table') }}

lower(coalesce(
nullif(url_tags, {{ dbt_utils.split_part('url', "'?'", 2) }}), '')
) as url_tags

from base
left join child_links
on base.id = child_links.creative_id

),

parsed as (

select

creative_id,
url,
links_joined.*,
{{ dbt_utils.split_part('url', "'?'", 1) }} as base_url,
{{ dbt_utils.get_url_host('url') }} as url_host,
'/' || {{dbt_utils.get_url_path('url') }} as url_path,

{{ dbt_utils.concat(["'/'", dbt_utils.get_url_path('url')]) }} as url_path,
{{ facebook_ads.get_url_parameter() }}

from base
from links_joined

)

Expand Down
101 changes: 101 additions & 0 deletions macros/stitch/base/stitch_fb_ad_creatives__child_links.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
-- There are cases where ads can have multiple links attached to them.
-- This model was created to extract all the child links and return the first
-- instance where a URL contains utm parameters.


{% macro stitch_fb_ad_creatives__child_links() %}

{{ adapter_macro('facebook_ads.stitch_fb_ad_creatives__child_links') }}

{% endmacro %}

{% macro default__stitch_fb_ad_creatives__child_links() %}


with base as (

select * from {{ var('ad_creatives__child_links_table') }}

),

child_attachment_links as (

select

_sdc_source_key_id as creative_id,
_sdc_batched_at,
link as child_link

from base
where child_link ilike '%utm%'

),

aggregated as (

select distinct

creative_id,
first_value(child_link) over (
partition by creative_id
order by _sdc_batched_at
rows between unbounded preceding and unbounded following
) as child_link

from child_attachment_links

)

select * from aggregated

{% endmacro %}


{% macro snowflake__stitch_fb_ad_creatives__child_links() %}


{% set fields = [

'object_story_spec',
'child_link',

]%}

with base as (

select * from {{ var('ad_creatives_table') }}

),

child_attachment_links as (

select
*,
attachments.value:link::varchar as child_link

from base,
lateral flatten (input => object_story_spec:link_data:child_attachments) attachments
where child_link ilike '%utm%'

),

aggregated as (

select distinct
id as creative_id,

{% for field in fields %}
first_value({{ field }}) over (partition by id
order by _sdc_batched_at
rows between unbounded preceding and unbounded following)
as {{ field }}
{% if not loop.last%} , {% endif %}
{% endfor %}

from child_attachment_links

)

select * from aggregated

{% endmacro %}
8 changes: 5 additions & 3 deletions models/router/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ models:
tests:
- not_null
- unique
- name: fb_ads_adsets
- name: fb_ad_creatives__child_links
columns:
- name: adset_id
- name: creative_id
tests:
- not_null
- relationships:
field: creative_id
to: ref('fb_ad_creatives')
- name: fb_ads
columns:
- name: creative_id
Expand Down
7 changes: 7 additions & 0 deletions models/router/stitch/fb_ad_creatives__child_links.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
enabled = var('etl') == 'stitch'
)
}}

{{ stitch_fb_ad_creatives__child_links() }}

0 comments on commit 2de1382

Please sign in to comment.