Script 237: switch strategies
Purpose
Python script to promote campaigns to a new Publisher Bidding Strategy if they meet minimum spend and ROAS requirements.
To Elaborate
The Python script solves the problem of automatically promoting campaigns to a new Publisher Bidding Strategy based on their performance. The key business rules are:
- If a campaign’s ROAS (Return on Ad Spend) is greater than or equal to a minimum threshold, it should be promoted.
- If a campaign’s spend is greater than or equal to a minimum threshold, it should be promoted.
- The new Publisher Bidding Strategy to be assigned is ‘TargetCpa’.
- The new Publisher Target CPA should be set to the campaign’s Cost per Conversion.
Walking Through the Code
- Set the minimum ROAS and the target Publisher Bidding Strategy as configurable parameters.
- Define column constants for the input and output dataframes.
- Create temporary columns in the input dataframe to store the new Publisher Bidding Strategy and Target CPA values.
- Assign the new Publisher Bidding Strategy as ‘TargetCpa’ to campaigns that meet the minimum ROAS threshold.
- Set the Target CPA value as the campaign’s Cost per Conversion.
- Identify the campaigns that have changed their Publisher Bidding Strategy.
- If there are changed campaigns, print a table of the campaigns with their changed Publisher Bidding Strategy.
- Create an output dataframe with the columns: Account, Campaign, Publisher Bidding Strategy, and Publisher Target CPA.
- Rename the temporary columns to match the output dataframe columns.
- Print the output dataframe.
- If there are no changed campaigns, print “Empty outputDf” and create an empty output dataframe.
Vitals
- Script ID : 237
- Client ID / Customer ID: 1306922587 / 2
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, Publisher Target CPA, Publisher Bid Strategy
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Byron Porter (bporter@marinsoftware.com)
- Created by Byron Porter on 2023-07-13 11:21
- Last Updated by Byron Porter on 2023-12-07 21:03
> See it in Action
Python Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
# Promote to new Publisher Bidding Strategy if meet minimum:
# - Spend
# - ROAS
#
#
# Author: Byron Porter
# Date: 2023-07-13
#
########### Configurable Params - START ##########
MIN_ROAS = 1
PUB_STRATEGY_TARGET = 'TargetCpa'
########### Configurable Params - END ###########
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_PUBLISHER_BIDSTRATEGY = 'Publisher Bid Strategy'
RPT_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
RPT_COL_ROAS = 'ROAS'
RPT_COL_CPC = 'Cost/Conv. $'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PUBLISHER_BIDSTRATEGY = 'Publisher Bid Strategy'
BULK_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
# temp columns for new values
TMP_PUB_STRATEGY = RPT_COL_PUBLISHER_BIDSTRATEGY + '_'
inputDf[TMP_PUB_STRATEGY] = np.nan
TMP_TARGETCPA = RPT_COL_PUBLISHER_TARGETCPA + '_'
inputDf[TMP_TARGETCPA] = np.nan
# if both Spend and ROAS meet mininum threshold, switch to new pub strategy
inputDf.loc[(inputDf[RPT_COL_ROAS] >= MIN_ROAS), \
TMP_PUB_STRATEGY \
] = PUB_STRATEGY_TARGET
inputDf[TMP_TARGETCPA] = inputDf[RPT_COL_CPC]
# find changed campaigns
changed = (inputDf[TMP_PUB_STRATEGY].notnull()) & (inputDf[RPT_COL_PUBLISHER_BIDSTRATEGY] != inputDf[TMP_PUB_STRATEGY])
if sum(changed) > 0:
print("== Campaigns with Changed Pub Strategy ==", tableize(inputDf.loc[changed]))
# only select changed rows
cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_PUB_STRATEGY, TMP_TARGETCPA]
outputDf = inputDf.loc[ changed, cols ].copy() \
.rename(columns = { \
TMP_PUB_STRATEGY: BULK_COL_PUBLISHER_BIDSTRATEGY, \
TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
})
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
outputDf = outputDf.iloc[0:0]
Post generated on 2024-05-15 07:44:05 GMT