Skip to content

Instantly share code, notes, and snippets.

from googleads import adwords
from googleads import oauth2
def connect_to_adwords_api():
customer_id = 'xxx-xxx-xxxx' # This is client customer ID you see in the google ads UI
oauth2_client = oauth2.GoogleRefreshTokenClient(
client_id={GOOGLE_CLIENT_ID},
client_secret={GOOGLE_CLIENT_SECRET},
refresh_token={GOOGLE_REFRESH_TOKEN}
def create_and_assign_budget():
adwords_client = connect_to_adwords_api()
# This bit here creates the budget
budget_service = adwords_client.GetService('BudgetService', version='v201809')
budget = {
'name': {an appropriate name}, # Doesn't need to be unique. Budget Id is what matters for uniqueness
'amount': {
'microAmount': int(round({budget_amount} * 1000000))
def create_draft():
adwords_client = connect_to_adwords_api()
draft_service = adwords_client.GetService('DraftService', version='v201809')
draft = {
'baseCampaignId': original_campaign_id,
'draftName': {draft_name}
}
draft_operation = {'operator': 'ADD', 'operand': draft}
draft = draft_service.mutate([draft_operation])['value'][0]
draft_campaign_id = draft['draftCampaignId']
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',
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
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),
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
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)
@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
@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%