-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0448158
commit 24349ab
Showing
7 changed files
with
1,767 additions
and
1 deletion.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
metricflow/test/integration/test_cases/itest_conversion_metric.yaml
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,88 @@ | ||
--- | ||
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__day | ||
, 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 | ||
FULL OUTER 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: ["visit__referrer_id"] | ||
check_query: | | ||
SELECT | ||
opportunities.referrer_id AS visit__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 | ||
FULL OUTER 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 |
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
Oops, something went wrong.