Skip to content

Instantly share code, notes, and snippets.

@limata3
limata3 / cazoo_attribution_model_sql_code.sql
Last active October 1, 2021 11:48
From Customer Touchpoints Table into Attributed Conversion Table
/* We will only be offering code samples for the original attribution models (we won;t have the to_lead and non_direct variations in here */
/* Note: We are using WITH statements in here, but in a real life example you would want to create actual tables for each step instead having each step as a WITH statement */
/* Note: This is based on BigQuery SQL dialect */
/* This starts with our customer touchpoints table - filtered by Customer XYZ only though */
WITH master_customer_touchpoints_and_ranking AS (
SELECT
/* Master Customer Touchpoints Table Original Fields */
customer_id
, device_id
@limata3
limata3 / cazoo_attrb_modelling_code.sql
Last active August 18, 2021 16:58
Distributing Credit amongst the different touchpoints for several attribution models
/* First Touch Attribution */
SELECT
customer_id
, order_id
, touchpoint_id
, CAST(MAX(total_net_orders) OVER (PARTITION BY buying_period_id) AS FLOAT64) AS first_touchpoint_credit
FROM master_ranked_customer_tocuhpoints
WHERE
touchpoints_customer_order_rank_asc = 1 /* This ensures all credit goes to the first touchpoint */
;
@limata3
limata3 / cazoo_attrb_modelling_rank_touchpoints.sql
Created August 18, 2021 16:47
Ranking customer touchpoints to be used in attribution modelling
SELECT
customer_id
, touchpoint_id
, lead_id
, order_id
, touchpoint_channel
, touchpoint_campaign
, touchpoint_datetime
, RANK() OVER (PARTITION BY customer_id ORDER BY touchpoint_datetime ASC, rand() ) AS touchpoints_customer_rank_asc /* touchpoint -> customer */
, RANK() OVER (PARTITION BY customer_id ORDER BY touchpoint_datetime DESC, rand() ) AS touchpoints_customer_rank_des /* touchpoint -> customer */
@limata3
limata3 / cazoo_attrb_modelling_example_channel_summary.csv
Created August 18, 2021 12:19
Distribution of credit amongst the different channels based on a cazoo customer journey example
Touchpoint Channel first_touchpoint_credit last_touchpoint_to_order_credit last_touchpoint_to_lead_credit linear_attribution_to_order_credit linear_attribution_to_lead_credit position_based_to_order_credit position_based_to_lead_credit last_touchpoint_to_order_non_dir_credit last_touchpoint_to_lead_non_dir_credit linear_attribution_to_order_non_dir_credit linear_attribution_to_lead_non_dir_credit position_based_to_order_non_dir_credit position_based_to_lead_non_dir_credit
Direct 0.00% 0.00% 0.00% 14.29% 0.00% 3.16% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00% 0.00%
Email 0.00% 0.00% 0.00% 71.43% 0.00% 15.79% 0.00% 0.00% 0.00% 83.33% 0.00% 18.75% 0.00%
Facebook Ads 0.00% 0.00% 100.00% 4.76% 50.00% 1.05% 50.00% 0.00% 100.00% 5.56% 50.00% 1.25% 50.00%
Generic Paid Search 100.00% 0.00% 0.00% 4.76% 50.00% 40.00% 50.00% 0.00% 0.00% 5.56% 50.00% 40.00% 50.00%
Organic Search 0.00% 100.00% 0.00% 4.76% 0.00% 40.00% 0.00% 100.00% 0.00% 5.56% 0.00% 40.00% 0.00%
@limata3
limata3 / cazoo_attrb_modelling_example.csv
Last active August 18, 2021 10:34
Different attribution models using a cazoo customer journey as exmaple
Customer Id Device Id Touchpoint Id Touchpoint Date Touchpoint Channel Touchpoint is Lead Touchpoint is Order first touchpoint credit last touchpoint to order credit last touchpoint to lead credit linear attribution to order credit linear attribution to lead credit position based to order credit position based to lead credit last touchpoint to order non dir credit last touchpoint to lead non dir credit linear attribution to order non dir credit linear attribution to lead non dir credit position based to order non dir credit position based to lead non dir credit
XYZ A 1 06/01/2021 Generic Paid Search 100.00% 0.00% 0.00% 4.76% 50.00% 40.00% 50.00% 0.00% 0.00% 5.56% 50.00% 40.00% 50.00%
XYZ B 2 07/01/2021 Facebook Ads TRUE 0.00% 0.00% 100.00% 4.76% 50.00% 1.05% 50.00% 0.00% 100.00% 5.56% 50.00% 1.25% 50.00%
XYZ A 3 07/01/2021 Email 0.00% 0.00% 0.00% 4.76% 0.00% 1.05% 0.00% 0.00% 0.00% 5.56% 0.00% 1.25% 0.00%
XYZ A 4 07/01/2021 Email 0.00% 0.00% 0.00% 4.76% 0.00% 1.05% 0.00% 0.00% 0.00% 5.56% 0.00% 1.25% 0
import pandas as pd
import numpy as np
raw_test_data = pd.read_csv('medium_ppc_inc_evaluate_dataset.csv')
# You can access the csv above in this link: https://drive.google.com/open?id=1IOuneJr-QFDYGsJPRjQ8ra-5C4VczeOW
raw_test_data['diff_conversions'] = raw_test_data['test_conversions'] - raw_test_data['ctl_conversions']
N = len(raw_test_data)
def create_experiment(experiment_name, base_campaign_id, draft_id, split_percent=50, split_type='COOKIE'):
adwords_client = connect_to_adwords_api()
trial_service = adwords_client.GetService('TrialService', version='v201809')
trial = {
'draftId': draft_id,
'baseCampaignId': base_campaign_id,
'name': experiment_name,
'trafficSplitPercent': split_percent,
'trafficSplitType': split_type
}
import pandas as pd
def get_all_ad_groups_from_campaign(campaign_id):
adwords_client = connect_to_adwords_api()
ad_group_service = adwords_client.GetService('AdGroupService', version='v201809')
selector = {
'fields': ['Id', 'Name', 'Status','TargetCpa'],
'paging': {
'startIndex': str(0),
with control_campaigns as (
select
'Control' as measurement_group
, campaign_name as base_campaign
, date
, cost
, conversions
, conversion_value
from
fivetran.adwords.google_campaign_performance
def change_draft_target_cpa(original_campaign_tcpa, original_campaign_name, draft_campaign_id):
# Create Portfolio for new campaign
draft_target_cpa = round(original_campaign_tcpa * (1-0.30), 2)
draft_portfolio_name = '{0}-Test_XYZ_Draft_Portfolio'.format(original_campaign_name)
adwords_client = connect_to_adwords_api()
bidding_strategy_service = adwords_client.GetService('BiddingStrategyService', version='v201809')
portfolio_config = {
'name': draft_portfolio_name,
'biddingScheme': {
'xsi_type': 'TargetCpaBiddingScheme',