Skip to content

Commit

Permalink
tests for conversion metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDee committed Jan 26, 2023
1 parent d2aba9a commit 0d20f37
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 2 deletions.
2 changes: 2 additions & 0 deletions metricflow/test/fixtures/model_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def template_mapping(mf_test_session_state: MetricFlowTestSessionState) -> Dict[
"source_schema": schema,
"accounts_source_table": f"{schema}.fct_accounts",
"primary_accounts_table": f"{schema}.dim_primary_accounts",
"buys_source_table": f"{schema}.fct_buys",
"visits_source_table": f"{schema}.fct_visits",
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
data_source:
name: buys_source
description: buys_source
owners:
- [email protected]

sql_query: |
-- User Defined SQL Query
SELECT * FROM $buys_source_table
measures:
- name: buys
expr: 1
agg: count
- name: buyers
expr: user_id
agg: count_distinct

dimensions:
- name: ds
type: time
type_params:
is_primary: True
time_granularity: day

identifiers:
- name: user
type: foreign
expr: user_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
data_source:
name: visits_source
description: visits_source
owners:
- [email protected]

sql_query: |
-- User Defined SQL Query
SELECT * FROM $visits_source_table
measures:
- name: visits
expr: 1
agg: count
- name: visitors
expr: user_id
agg: count_distinct


dimensions:
- name: ds
type: time
type_params:
is_primary: True
time_granularity: day
- name: referrer_id
type: categorical

identifiers:
- name: user
type: foreign
expr: user_id
28 changes: 28 additions & 0 deletions metricflow/test/fixtures/model_yamls/simple_model/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,31 @@ metric:
- name: bookings
offset_window: 5 days
alias: bookings_5_days_ago
---
metric:
name: visit_buy_conversion_rate
description: conversion rate on visits-buys on a 7 day window
owners:
- [email protected]
type: conversion
type_params:
conversion_type_params:
base_measure: visits
conversion_measure: buys
window: 7 days
entity: user
calculation: conversion_rate
---
metric:
name: visit_buy_conversions
description: conversion count on visits-buys on a 7 day window
owners:
- [email protected]
type: conversion
type_params:
conversion_type_params:
base_measure: visits
conversion_measure: buys
window: 7 days
entity: user
calculation: conversions
55 changes: 55 additions & 0 deletions metricflow/test/fixtures/table_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,61 @@ def create_simple_model_tables(mf_test_session_state: MetricFlowTestSessionState
),
)

# Events data
visits_data = [
("u0004114", "2020-01-01", "fb_ad_1"),
("u0004214", "2020-01-01", "fb_ad_2"),
("u0003141", "2020-01-01", "homepage_1"),
("u0003154", "2020-01-01", "homepage_2"),
("u1612112", "2020-01-02", "homepage_1"),
("u0042324", "2020-01-02", "homepage_1"),
("u0005432", "2020-01-02", "fb_ad_3"),
("u0003452", "2020-01-02", "fb_ad_1"),
("u0003452", "2020-01-02", "user_2"),
("u0042324", "2020-01-03", "fb_ad_1"),
("u0005432", "2020-01-03", "google_ad_1"),
("u0005472", "2020-01-03", "google_ad_2"),
("u0005414", "2020-01-04", "google_ad_1"),
("u0004114", "2020-01-06", "homepage_1"),
("u0004114", "2020-01-07", "fb_ad_2"),
("u0004117", "2020-01-10", "google_ad_1"),
("u0003141", "2020-01-12", "user_1"),
]

create_table(
sql_client=sql_client,
sql_table=SqlTable(schema_name=schema, table_name="fct_visits"),
df=make_df(
sql_client=sql_client,
columns=["user_id", DEFAULT_DS, "referrer_id"],
time_columns={DEFAULT_DS},
data=visits_data,
),
)

buy_data = [
("u0004114", "2020-01-02"),
("u0042324", "2020-01-03"),
("u0042324", "2020-01-03"),
("u0005432", "2020-01-04"),
("u1612112", "2020-01-07"),
("u0004114", "2020-01-07"),
("u0004117", "2020-01-10"),
("u0003141", "2020-01-07"),
("u0003452", "2020-01-04"),
]

create_table(
sql_client=sql_client,
sql_table=SqlTable(schema_name=schema, table_name="fct_buys"),
df=make_df(
sql_client=sql_client,
columns=["user_id", DEFAULT_DS],
time_columns={DEFAULT_DS},
data=buy_data,
),
)

return True


Expand Down
230 changes: 230 additions & 0 deletions metricflow/test/integration/test_cases/itest_conversion_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
---
integration_test:
name: conversion_rate_metric
description: Query a conversion metric that calculates the conversion rate
model: SIMPLE_MODEL
metrics: ["visit_buy_conversion_rate"]
group_bys: ["metric_time"]
check_query: |
SELECT
opportunities.metric_time AS metric_time
, CAST(conversions.buys AS {{ double_data_type_name }}) / CAST(NULLIF(opportunities.visits, 0) AS {{ double_data_type_name }}) AS visit_buy_conversion_rate
FROM (
SELECT
metric_time, SUM(a.visits) AS visits
FROM (
SELECT
ds AS metric_time, 1 AS visits
FROM {{ source_schema }}.fct_visits visits
) a
GROUP BY
a.metric_time
) opportunities
INNER JOIN (
SELECT
b.ds AS metric_time, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > {{ render_date_sub("buy_source", "ds", 7, TimeGranularity.DAY) }}
) b
GROUP BY
b.ds
) conversions
ON opportunities.metric_time = conversions.metric_time
---
integration_test:
name: conversion_rate_metric_with_dimension
description: Query a conversion metric that calculates the conversion rate without time dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversion_rate"]
group_bys: ["referrer_id"]
check_query: |
SELECT
opportunities.referrer_id AS referrer_id
, CAST(conversions.buys AS {{ double_data_type_name }}) / CAST(NULLIF(opportunities.visits, 0) AS {{ double_data_type_name }}) AS visit_buy_conversion_rate
FROM (
SELECT
referrer_id, SUM(a.visits) AS visits
FROM (
SELECT
referrer_id, 1 AS visits
FROM {{ source_schema }}.fct_visits visits
) a
GROUP BY
a.referrer_id
) opportunities
INNER JOIN (
SELECT
referrer_id AS referrer_id, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > buy_source.ds - INTERVAL 7 day
) b
GROUP BY
b.referrer_id
) conversions
ON opportunities.referrer_id = conversions.referrer_id
---
integration_test:
name: conversion_rate_metric_with_multiple_dimension
description: Query a conversion metric that calculates the conversion rate with multiple dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversion_rate"]
group_bys: ["metric_time", "referrer_id"]
check_query: |
SELECT
opportunities.referrer_id AS referrer_id
, opportunities.metric_time AS metric_time
, CAST(conversions.buys AS {{ double_data_type_name }}) / CAST(NULLIF(opportunities.visits, 0) AS {{ double_data_type_name }}) AS visit_buy_conversion_rate
FROM (
SELECT
metric_time, referrer_id, SUM(a.visits) AS visits
FROM (
SELECT
ds AS metric_time, referrer_id, 1 AS visits
FROM {{ source_schema }}.fct_visits visits
) a
GROUP BY
a.referrer_id, a.metric_time
) opportunities
INNER JOIN (
SELECT
b.ds AS metric_time, referrer_id AS referrer_id, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > buy_source.ds - INTERVAL 7 day
) b
GROUP BY
b.referrer_id, b.ds
) conversions
ON opportunities.referrer_id = conversions.referrer_id AND opportunities.metric_time = conversions.metric_time
---
integration_test:
name: conversion_count_metric
description: Query a conversion metric that calculates the conversion count
model: SIMPLE_MODEL
metrics: ["visit_buy_conversions"]
group_bys: ["metric_time"]
check_query: |
SELECT
conversions.metric_time AS metric_time
, conversions.buys AS visit_buy_conversions
FROM (
SELECT
b.ds AS metric_time, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > buy_source.ds - INTERVAL 7 day
) b
GROUP BY
b.ds
) conversions
---
integration_test:
name: conversion_count_metric_with_dimension
description: Query a conversion metric that calculates the conversion count without time dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversions"]
group_bys: ["referrer_id"]
check_query: |
SELECT
conversions.referrer_id AS referrer_id
, conversions.buys AS visit_buy_conversions
FROM (
SELECT
referrer_id AS referrer_id, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > buy_source.ds - INTERVAL 7 day
) b
GROUP BY
b.referrer_id
) conversions
---
integration_test:
name: conversion_count_metric_with_multiple_dimension
description: Query a conversion metric that calculates the conversion count with multiple dimension
model: SIMPLE_MODEL
metrics: ["visit_buy_conversions"]
group_bys: ["metric_time", "referrer_id"]
check_query: |
SELECT
conversions.referrer_id AS referrer_id
, conversions.metric_time AS metric_time
, conversions.buys AS visit_buy_conversions
FROM (
SELECT
b.ds AS metric_time, referrer_id AS referrer_id, SUM(b.buys) AS buys
FROM (
SELECT DISTINCT
first_value(v.ds) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS ds
, first_value(v.user_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS user_id
, first_value(v.referrer_id) OVER (PARTITION BY buy_source.ds, buy_source.user_id ORDER BY v.ds DESC NULLS FIRST) AS referrer_id
, buy_source.uuid
, 1 AS buys
FROM {{ source_schema }}.fct_visits v
INNER JOIN
(
SELECT *, {{ generate_random_uuid() }} AS uuid FROM {{ source_schema }}.fct_buys
) buy_source
ON
v.user_id = buy_source.user_id AND v.ds <= buy_source.ds AND v.ds > buy_source.ds - INTERVAL 7 day
) b
GROUP BY
b.referrer_id, b.ds
) conversions
Loading

0 comments on commit 0d20f37

Please sign in to comment.