From 8b585a2787801a86dae9f66a0baf6f052422ff4e Mon Sep 17 00:00:00 2001 From: Son Do Date: Tue, 9 Apr 2024 17:12:41 -0700 Subject: [PATCH] Added Retention for bam chains except solana --- macros/retention/get_cohort_retention.sql | 79 +++++++++++++++++++ models/data_hubs/fact_retention_datahub.sql | 17 ++++ .../arbitrum/core/ez_arbitrum_retention.sql | 11 +++ .../avalanche/core/ez_avalanche_retention.sql | 11 +++ .../projects/base/core/ez_base_retention.sql | 11 +++ models/projects/bsc/core/ez_bsc_retention.sql | 11 +++ .../ethereum/core/ez_ethereum_retention.sql | 11 +++ .../projects/near/core/ez_near_retention.sql | 11 +++ .../optimism/core/ez_optimism_retention.sql | 11 +++ .../polygon/core/ez_polygon_retention.sql | 11 +++ .../projects/tron/core/ez_tron_retention.sql | 11 +++ 11 files changed, 195 insertions(+) create mode 100644 macros/retention/get_cohort_retention.sql create mode 100644 models/data_hubs/fact_retention_datahub.sql create mode 100644 models/projects/arbitrum/core/ez_arbitrum_retention.sql create mode 100644 models/projects/avalanche/core/ez_avalanche_retention.sql create mode 100644 models/projects/base/core/ez_base_retention.sql create mode 100644 models/projects/bsc/core/ez_bsc_retention.sql create mode 100644 models/projects/ethereum/core/ez_ethereum_retention.sql create mode 100644 models/projects/near/core/ez_near_retention.sql create mode 100644 models/projects/optimism/core/ez_optimism_retention.sql create mode 100644 models/projects/polygon/core/ez_polygon_retention.sql create mode 100644 models/projects/tron/core/ez_tron_retention.sql diff --git a/macros/retention/get_cohort_retention.sql b/macros/retention/get_cohort_retention.sql new file mode 100644 index 00000000..d1ba344e --- /dev/null +++ b/macros/retention/get_cohort_retention.sql @@ -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 %} \ No newline at end of file diff --git a/models/data_hubs/fact_retention_datahub.sql b/models/data_hubs/fact_retention_datahub.sql new file mode 100644 index 00000000..c908bcd2 --- /dev/null +++ b/models/data_hubs/fact_retention_datahub.sql @@ -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") + ], + ) +}} \ No newline at end of file diff --git a/models/projects/arbitrum/core/ez_arbitrum_retention.sql b/models/projects/arbitrum/core/ez_arbitrum_retention.sql new file mode 100644 index 00000000..80e4ffb6 --- /dev/null +++ b/models/projects/arbitrum/core/ez_arbitrum_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="arbitrum", + database="arbitrum", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("arbitrum") }} \ No newline at end of file diff --git a/models/projects/avalanche/core/ez_avalanche_retention.sql b/models/projects/avalanche/core/ez_avalanche_retention.sql new file mode 100644 index 00000000..d1083696 --- /dev/null +++ b/models/projects/avalanche/core/ez_avalanche_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="avalanche", + database="avalanche", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("avalanche") }} \ No newline at end of file diff --git a/models/projects/base/core/ez_base_retention.sql b/models/projects/base/core/ez_base_retention.sql new file mode 100644 index 00000000..5fb388bf --- /dev/null +++ b/models/projects/base/core/ez_base_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="base", + database="base", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("base") }} \ No newline at end of file diff --git a/models/projects/bsc/core/ez_bsc_retention.sql b/models/projects/bsc/core/ez_bsc_retention.sql new file mode 100644 index 00000000..f2c8f5f6 --- /dev/null +++ b/models/projects/bsc/core/ez_bsc_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="bsc", + database="bsc", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("bsc") }} \ No newline at end of file diff --git a/models/projects/ethereum/core/ez_ethereum_retention.sql b/models/projects/ethereum/core/ez_ethereum_retention.sql new file mode 100644 index 00000000..bad9865a --- /dev/null +++ b/models/projects/ethereum/core/ez_ethereum_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="ethereum", + database="ethereum", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("ethereum") }} \ No newline at end of file diff --git a/models/projects/near/core/ez_near_retention.sql b/models/projects/near/core/ez_near_retention.sql new file mode 100644 index 00000000..0a7a63f2 --- /dev/null +++ b/models/projects/near/core/ez_near_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="near", + database="near", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("near") }} \ No newline at end of file diff --git a/models/projects/optimism/core/ez_optimism_retention.sql b/models/projects/optimism/core/ez_optimism_retention.sql new file mode 100644 index 00000000..1a78ff2d --- /dev/null +++ b/models/projects/optimism/core/ez_optimism_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="optimism", + database="optimism", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("optimism") }} \ No newline at end of file diff --git a/models/projects/polygon/core/ez_polygon_retention.sql b/models/projects/polygon/core/ez_polygon_retention.sql new file mode 100644 index 00000000..e7db17d2 --- /dev/null +++ b/models/projects/polygon/core/ez_polygon_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="polygon", + database="polygon", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("polygon") }} \ No newline at end of file diff --git a/models/projects/tron/core/ez_tron_retention.sql b/models/projects/tron/core/ez_tron_retention.sql new file mode 100644 index 00000000..cd31cd2d --- /dev/null +++ b/models/projects/tron/core/ez_tron_retention.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="tron", + database="tron", + schema="core", + alias="ez_cohort_retention", + ) + }} + +{{ get_cohort_retention("tron") }} \ No newline at end of file