Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions macros/agg_automation_activities.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{% macro agg_automation_activities(by) %}

{% set id_cols = [] %}
{% for col in by %}
{% do id_cols.append(col ~ "_id" ) %}
{% endfor %}

with activities as (

select *
from {{ ref('mailchimp__automation_activities')}}

), recipients as (

select *
from {{ ref('int_mailchimp__automation_recipients') }}

), unsubscribes as (

select *
from {{ ref('int_mailchimp__automation_unsubscribes') }}


-- aggregate automation opens and clicks by {{ by }}

), pivoted as (

select
{% for id_col in id_cols %}
{{ id_col }},
{% endfor %}
sum(case when action_type = 'open' then 1 end) as opens,
sum(case when action_type = 'click' then 1 end) as clicks,
count(distinct case when action_type = 'open' then member_id end) as unique_opens,
count(distinct case when action_type = 'click' then member_id end) as unique_clicks
from activities
where
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}{{ id_col }} is not null
{% endfor %}
group by
{% for id_col in id_cols %}
{% if not loop.first %}, {% endif %}{{ id_col }}
{% endfor %}

), sends as (

select
{% for id_col in id_cols %}
{{ id_col }},
{% endfor %}
count(*) as sends
from recipients
where
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}{{ id_col }} is not null
{% endfor %}
group by
{% for id_col in id_cols %}
{% if not loop.first %}, {% endif %}{{ id_col }}
{% endfor %}

), unsubscribes_xf as (

select
{% for id_col in id_cols %}
{{ id_col }},
{% endfor %}
count(*) as unsubscribes
from unsubscribes
where
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}{{ id_col }} is not null
{% endfor %}
group by
{% for id_col in id_cols %}
{% if not loop.first %}, {% endif %}{{ id_col }}
{% endfor %}

), joined as (

select
{% for id_col in id_cols %}
sends.{{ id_col }},
{% endfor %}
pivoted.opens,
pivoted.clicks,
pivoted.unique_opens,
pivoted.unique_clicks,
sends.sends,
unsubscribes_xf.unsubscribes
from sends
left join pivoted on
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}sends.{{ id_col }} = pivoted.{{ id_col }}
{% endfor %}
full outer join unsubscribes_xf on
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}sends.{{ id_col }} = unsubscribes_xf.{{ id_col }}
{% endfor %}

)

select *
from joined

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{{ config(enabled=var('mailchimp_using_segments', True)) }}
{% macro agg_campaign_activities(by) %}

{% set id_cols = [] %}
{% for col in by %}
{% do id_cols.append(col ~ "_id" ) %}
{% endfor %}

with recipients as (

Expand All @@ -8,18 +13,28 @@ with recipients as (
), pivoted as (

select
segment_id,
{% for id_col in id_cols %}
{{ id_col }},
{% endfor %}
count(*) as sends,
sum(opens) as opens,
sum(clicks) as clicks,
count(distinct case when was_opened = True then member_id end) as unique_opens,
count(distinct case when was_clicked = True then member_id end) as unique_clicks,
count(distinct case when was_unsubscribed = True then member_id end) as unsubscribes
from recipients
where segment_id is not null
group by 1
where
{% for id_col in id_cols %}
{% if not loop.first %}and {% endif %}{{ id_col }} is not null
{% endfor %}
group by
{% for id_col in id_cols %}
{% if not loop.first %}, {% endif %}{{ id_col }}
{% endfor %}

)

select *
from pivoted
from pivoted

{% endmacro %}

This file was deleted.

This file was deleted.

This file was deleted.

Loading