Build loyalty points analytics with dbt analyzing program engagement and metrics
harbor run -t snowflake-labs/dbt-loyalty-points-analysisharbor download snowflake-labs/dbt-loyalty-points-analysisBuild a dbt project that analyzes loyalty points transactions by program to measure engagement and liability.
This task supports both DuckDB and Snowflake. Run echo $DB_TYPE in a shell BEFORE you write any code or spawn any subagents — the live env determines which backend the verifier runs against. Do NOT assume a default from this prose. Both /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ exist on disk; the verifier only checks the project matching the live $DB_TYPE.
DB_TYPE=duckdb$DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflakeSNOWFLAKE_ACCOUNTSNOWFLAKE_USERSNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE - The clone database to useSNOWFLAKE_SCHEMASNOWFLAKE_WAREHOUSESNOWFLAKE_ROLE (optional)Note: For Snowflake, the entrypoint automatically creates a clone database and sets SNOWFLAKE_DATABASE. The clone is destroyed when the task completes.
/app/database/retail.duckdb/app/dbt_project (DuckDB) or /app/dbt_models_snowflake (Snowflake)loyalty_analyticsYou must configure dbt to connect to the database:
profiles.yml in the dbt project directory with the appropriate profile nameDBT_PROFILES_DIR environment variable to the directory containing profiles.ymlConfigure with type: duckdb and the database path from $DUCKDB_PATH.
Configure with type: snowflake using password authentication:
retail_dw_masterThe database contains the following tables in the main schema:
LOYALTY_POINTS_TRANSACTIONS - Transaction records with: transaction_id, customer_id, program_id, transaction_type, points, balance_after, order_id, description, expires_at, created_atLOYALTY_PROGRAMS - Program details: program_id, program_name, program_type, points_per_dollar, points_value, is_activeCUSTOMERS - Customer information: customer_id, first_name, last_name, email, statusLOYALTY_POINTS_TRANSACTIONS in the MARKETING schemaLOYALTY_PROGRAMS in the MARKETING schemaCUSTOMERS in the CUSTOMER schemaTransaction types include: EARN, REDEEM, EXPIRE, ADJUST, BONUS
Create a dbt project with the following structure:
profiles.yml to connect to the appropriate databaseloyalty_analytics in profiles.yml only (not in dbt_project.yml)Create staging models for transactions, programs, and customers:
stg_lp_transactions.sql — staging for loyalty points transactionsstg_lp_programs.sql — staging for loyalty programsstg_lp_customers.sql — staging for customersEach staging model should include:
Create int_loyalty_metrics.sql that aggregates transaction data by program:
SUM(ABS(points)) over REDEEM transactions — take the absolute value of each REDEEM transaction's points, then sum (i.e. SUM(ABS(points)), NOT ABS(SUM(points)))SUM(ABS(points)) over EXPIRE transactions — take the absolute value of each EXPIRE transaction's points, then sum (i.e. SUM(ABS(points)), NOT ABS(SUM(points)))Create program_loyalty_summary.sql as a table with:
program_id - program identifierprogram_name - name from programs tablemember_count - distinct customers with transactionspoints_earned - total points earnedpoints_redeemed - total redeemed (positive value)points_expired - total expired (positive value)points_balance - remaining liability, calculated as points_earned − points_redeemed − points_expired (do NOT include ADJUST transactions)redemption_rate - percentage of earned points that were redeemedavg_points_per_member - average balance per member (integer)