Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new view for aggregated stat on purchase propensity predictions #268

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions infrastructure/terraform/modules/monitor/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ locals {
activation_project_url = "${local.p_key}=${var.activation_project_id}"

mds_dataform_repo = "marketing-analytics"

purchase_propensity_dataset = "purchase_propensity"
}

module "project_services" {
Expand Down Expand Up @@ -259,3 +261,29 @@ data "template_file" "looker_studio_dashboard_url" {
dataflow_log_table_id = local.dataflow_log_table_id
}
}

data "template_file" "purchase_propensity_prediction_stats_query" {
template = file("${local.source_root_dir}/templates/purchase_propensity_smart_bidding_view.sql.tpl")
vars = {
project_id = var.feature_store_project_id
purchase_propensity_dataset = local.purchase_propensity_dataset
activation_dataset = "activation"
}
}

data "google_bigquery_dataset" "purchase_propensity_dataset" {
dataset_id = local.purchase_propensity_dataset
project = var.feature_store_project_id
}

resource "google_bigquery_table" "purchase_propensity_prediction_stats" {
project = var.feature_store_project_id
dataset_id = data.google_bigquery_dataset.purchase_propensity_dataset.dataset_id
table_id = "purchase_propensity_prediction_stats"
deletion_protection = false

view {
query = data.template_file.purchase_propensity_prediction_stats_query.rendered
use_legacy_sql = false
}
}
41 changes: 41 additions & 0 deletions templates/purchase_propensity_smart_bidding_view.sql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Copyright 2024 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

SELECT
p_stat.inference_date,
p_stat.p_p_decile,
p_stat.number_of_users,
conf.value*p_stat.number_of_users AS predicted_purchase_value
FROM (
SELECT
inference_date,
p_p_decile,
COUNT(p_p_decile) AS number_of_users
FROM (
SELECT
PARSE_DATE('%Y_%m_%d', SUBSTR(_TABLE_SUFFIX, 1,10)) AS inference_date,
NTILE(10) OVER (PARTITION BY _TABLE_SUFFIX ORDER BY b.prediction_prob DESC) AS p_p_decile,
FROM
`${project_id}.${purchase_propensity_dataset}.predictions_*` b
WHERE
ENDS_WITH(_TABLE_SUFFIX, '_view') )
GROUP BY
inference_date,
p_p_decile ) AS p_stat
JOIN
`${project_id}.${activation_dataset}.vbb_activation_configuration` conf
ON
p_stat.p_p_decile = decile
WHERE
conf.activation_type = 'purchase-propensity'
Loading