Skip to content

Commit

Permalink
Merge pull request #16 from Artemis-xyz/son/chain-retention
Browse files Browse the repository at this point in the history
Added Retention for Activity Monitor chains except solana
  • Loading branch information
Sunny77D authored Apr 10, 2024
2 parents 636de38 + 8b585a2 commit 1bebab0
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 0 deletions.
79 changes: 79 additions & 0 deletions macros/retention/get_cohort_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{% macro get_cohort_retention(chain) %}
with
min_date as (
select
date_trunc('month', min(raw_date)) as first_month
from {{ ref( "ez_" ~ chain ~ "_transactions") }}
),
base_table as(
select
date_trunc('month', raw_date) as base_month,
from_address as address
from {{ ref( "ez_" ~ chain ~ "_transactions") }}
group by 1,2
),
-- from user_cohorts onward, should be able to reuse ctes on any chain

-- grab each users first action timestamp on chain
user_cohorts as (
select
address,
min(base_month) as cohort_month
from
base_table
group by 1
),

--compute cohort size per distinct cohort period
cohort_size as (
select
cohort_month,
count(distinct(address)) as cohort_size
from
user_cohorts
group by 1
),

-- determine if/ when users came back for another interaction based on period number
following_months as (
select
bt.address,
timestampdiff(month, uc.cohort_month, bt.base_month) as month_number
from
base_table as bt
inner join user_cohorts as uc on bt.address = uc.address
where
bt.base_month > uc.cohort_month
group by
bt.address,
month_number
),
--aggregate and calcualte the retained user amount per time period per cohort
retention_data as(
select
uc.cohort_month as cohort_month,
fm.month_number,
count(distinct(fm.address)) as retained_user_count
from
following_months as fm
inner join user_cohorts as uc on fm.address = uc.address
where
cohort_month >= (select first_month from min_date) -- Grab data from the beginning of the month 24 months ago
group by
uc.cohort_month,
fm.month_number
)
select
'{{ chain }}' as chain,
r.cohort_month,
c.cohort_size,
r.month_number,
round(r.retained_user_count::numeric / c.cohort_size::numeric , 2) as retention_ratio
from
retention_data as r
inner join cohort_size as c on r.cohort_month = c.cohort_month
order by
r.cohort_month,
r.month_number

{% endmacro %}
17 changes: 17 additions & 0 deletions models/data_hubs/fact_retention_datahub.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{ config(materialized="table", snowflake_warehouse="RETENTION") }}

{{
dbt_utils.union_relations(
relations=[
ref("ez_arbitrum_retention"),
ref("ez_avalanche_retention"),
ref("ez_base_retention"),
ref("ez_bsc_retention"),
ref("ez_ethereum_retention"),
ref("ez_near_retention"),
ref("ez_optimism_retention"),
ref("ez_polygon_retention"),
ref("ez_tron_retention")
],
)
}}
11 changes: 11 additions & 0 deletions models/projects/arbitrum/core/ez_arbitrum_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="arbitrum",
database="arbitrum",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("arbitrum") }}
11 changes: 11 additions & 0 deletions models/projects/avalanche/core/ez_avalanche_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="avalanche",
database="avalanche",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("avalanche") }}
11 changes: 11 additions & 0 deletions models/projects/base/core/ez_base_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="base",
database="base",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("base") }}
11 changes: 11 additions & 0 deletions models/projects/bsc/core/ez_bsc_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="bsc",
database="bsc",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("bsc") }}
11 changes: 11 additions & 0 deletions models/projects/ethereum/core/ez_ethereum_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="ethereum",
database="ethereum",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("ethereum") }}
11 changes: 11 additions & 0 deletions models/projects/near/core/ez_near_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="near",
database="near",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("near") }}
11 changes: 11 additions & 0 deletions models/projects/optimism/core/ez_optimism_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="optimism",
database="optimism",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("optimism") }}
11 changes: 11 additions & 0 deletions models/projects/polygon/core/ez_polygon_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="polygon",
database="polygon",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("polygon") }}
11 changes: 11 additions & 0 deletions models/projects/tron/core/ez_tron_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized="table",
snowflake_warehouse="tron",
database="tron",
schema="core",
alias="ez_cohort_retention",
)
}}

{{ get_cohort_retention("tron") }}

0 comments on commit 1bebab0

Please sign in to comment.