Skip to content

Commit

Permalink
Merge branch 'GoogleCloudPlatform:main' into lead-score-propensity
Browse files Browse the repository at this point in the history
  • Loading branch information
chmstimoteo authored Dec 18, 2024
2 parents 3c9a954 + 4a6edcc commit 40de2a5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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'

0 comments on commit 40de2a5

Please sign in to comment.