This file contains hidden or 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
/* 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 |
This file contains hidden or 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
/* 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 */ | |
; |
This file contains hidden or 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
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 */ |
This file contains hidden or 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
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% | |
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% |
This file contains hidden or 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
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 | 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 | 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 |
This file contains hidden or 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
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) |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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), |
This file contains hidden or 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
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 |
This file contains hidden or 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
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', |
NewerOlder