-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mjirv/feat-add_retention
Feat: adds retention analysis
- Loading branch information
Showing
12 changed files
with
145 additions
and
19 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
|
||
target/ | ||
dbt_modules/ | ||
dbt_packages/ | ||
logs/ | ||
**/.DS_Store |
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
6 changes: 6 additions & 0 deletions
6
integration_tests/models/product_analytics/retention_orders.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,6 @@ | ||
{{ dbt_product_analytics.retention( | ||
event_stream=ref('order_events'), | ||
first_action='completed', | ||
second_action='completed', | ||
start_date='2018-01-17' | ||
)}} |
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
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 @@ | ||
dbt clean && dbt deps && dbt seed && dbt run && dbt test |
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,2 @@ | ||
unique_users_day_0,unique_users_day_1,unique_users_day_7,unique_users_day_14,unique_users_day_30,unique_users_day_60,unique_users_day_120 | ||
2,0,0,0,0,0,1 |
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
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
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,11 @@ | ||
{% macro _dateadd(datepart, interval, from_date_or_timestamp) %} | ||
{{ return(adapter.dispatch('_dateadd', 'dbt_product_analytics')(datepart, interval, from_date_or_timestamp)) }} | ||
{% endmacro %} | ||
|
||
{% macro default___dateadd(datepart, interval, from_date_or_timestamp) %} | ||
{{ return(adapter.dispatch('dateadd', 'dbt_utils')(datepart, interval, from_date_or_timestamp)) }} | ||
{% endmacro %} | ||
|
||
{% macro trino___dateadd(datepart, interval, from_date_or_timestamp) %} | ||
{{ from_date_or_timestamp }} + interval '{{ interval }}' {{ datepart }} | ||
{% endmacro %} |
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 @@ | ||
{% macro _select_event_stream(event_stream) -%} | ||
( {% if not (event_stream|string|trim).startswith('select ') %} select * from {% endif %} {{ event_stream }} ) | ||
{%- endmacro %} |
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,71 @@ | ||
{% macro retention(event_stream=None, first_action=None, second_action=None, start_date=None, periods=[1,7,14,30,60,120], period_type='day', dimensions=[]) %} | ||
{% if event_stream is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'event_stream\' must be provided')}} | ||
{% endif %} | ||
|
||
{% if first_action is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'first_action\' must be provided')}} | ||
{% endif %} | ||
|
||
{% if second_action is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'second_action\' must be provided')}} | ||
{% endif %} | ||
|
||
{% if start_date is none %} | ||
{{ exceptions.raise_compiler_error('parameter \'start_date\' must be provided')}} | ||
{% endif %} | ||
|
||
with event_stream as {{ dbt_product_analytics._select_event_stream(event_stream) }} | ||
|
||
, first_events as ( | ||
select distinct user_id | ||
{% for dimension in dimensions %}, {{ dimension }} {% endfor %} | ||
from event_stream | ||
where event_type = '{{ first_action }}' | ||
and event_date = cast('{{ start_date }}' as date) | ||
) | ||
|
||
, first_event_counts as ( | ||
select | ||
{% for dimension in dimensions %} {{ dimension }}, {% endfor %} | ||
count(*) as unique_users_{{ period_type }}_0 | ||
from first_events | ||
{% for dimension in dimensions -%} | ||
{% if loop.first %} group by {% endif %} {{ loop.index }} | ||
{%- endfor %} | ||
) | ||
|
||
{% for period in periods %} | ||
, secondary_events_{{ period }} as ( | ||
select {% for dimension in dimensions %} {{ dimension }}, {% endfor %} | ||
count(distinct user_id) as unique_users_{{ period_type }}_{{ period }} | ||
from event_stream | ||
where event_type = '{{ second_action }}' | ||
and event_date > cast('{{ start_date }}' as date) | ||
and event_date < {{ dbt_product_analytics._dateadd(datepart=period_type, interval=period, from_date_or_timestamp="cast('" ~ start_date ~ "' as date)") }} | ||
and user_id in ( | ||
select user_id from first_events | ||
) | ||
{% for dimension in dimensions -%} | ||
{% if loop.first %} group by {% endif %} {{ loop.index }} | ||
{%- endfor %} | ||
) | ||
{% endfor %} | ||
|
||
, final as ( | ||
select | ||
{% for dimension in dimensions %} {{ dimension }}, {% endfor %} | ||
unique_users_{{ period_type }}_0, | ||
{% for period in periods %} unique_users_{{ period_type }}_{{ period }} {% if not loop.last %}, {% endif %} {% endfor %} | ||
from first_event_counts | ||
{% for period in periods %} | ||
left join secondary_events_{{ period }} | ||
on 1 = 1 | ||
{% for dimension in dimensions %} | ||
and first_event_counts.{{ dimension }} = secondary_events_{{ period }}.{{ dimension }} | ||
{% endfor %} | ||
{% endfor %} | ||
) | ||
|
||
select * from final | ||
{% endmacro %} |
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 @@ | ||
packages: | ||
- package: dbt-labs/dbt_utils | ||
version: 0.8.6 |