Script 409: New Launch Updates Round 2

Purpose

Python script to update the status, tCPA, bid strategy, and campaign maturity for new launch campaigns.

To Elaborate

This Python script is designed to update certain parameters for new launch campaigns. The parameters that can be updated include the campaign status, target cost per acquisition (tCPA), daily budget, and campaign maturity. The script applies specific criteria to determine the updates for each campaign based on their current performance metrics, such as the return on ad spend (ROAS). The script then generates an output dataframe that contains the campaigns with updated parameters.

Walking Through the Code

  1. The script defines column constants for various data columns used in the code.
  2. It sets the current maturity parameter value based on the input dataframe.
  3. The script defines a list of tuples that represent the thresholds/conditions for updating the new launch campaign values. Each tuple contains the minimum ROAS, maximum ROAS, budget adjustment, tCPA adjustment, and desired status for the campaigns.
  4. Temporary columns are created in the input dataframe to store the new values and ensure that all previous values are cleared out.
  5. The script loops through each tuple in the new launch adjustment criteria list and applies the criteria to the input dataframe.
  6. For each tuple, the script identifies the campaigns that meet the criteria based on their ROAS.
  7. If there are campaigns that match the criteria, the script updates the daily budget, tCPA, and status columns in the input dataframe with the new values.
  8. The script prints the campaigns that had the adjustments applied.
  9. The script then identifies the campaigns that have changed parameters by comparing the original values with the temporary columns.
  10. If there are changed campaigns, the script creates an output dataframe that contains the changed campaigns and their updated parameters.
  11. The output dataframe is printed.

Vitals

  • Script ID : 409
  • Client ID / Customer ID: 1306925431 / 60269477
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Status, Daily Budget, Publisher Target CPA
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-10-24 21:53
  • Last Updated by simon@rainmakeradventures.com on 2023-12-06 04:01
> 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#
# Update, Status, tCPA, Bid Strategy, and or Campaign Maturity for New Launch Campaigns
#  
#  
#
#
# Author: Byron Porter
# Date: 2023-10-16
#


RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_PUBLISHER_BIDSTRATEGY = 'Publisher Bid Strategy'
RPT_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_REVENUE = 'Revenue $'
RPT_COL_ROAS = 'CLICKS ROAS'
RPT_COL_RPC = 'Rev./Click $'
RPT_COL_CLICKS = 'Clicks'
RPT_COL_MATURITY = 'Campaign Maturity'

BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STATUS = 'Status'
BULK_COL_DAILY_BUDGET = 'Daily Budget'
BULK_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
BULK_COL_PUBLISHER_BIDSTRATEGY = 'Publisher Bid Strategy'
BULK_COL_MATURITY = 'Campaign Maturity'


#set maturity parameter value so that it can be used in tuple
current_maturity = inputDf[RPT_COL_MATURITY]

# Define thesholds/conditions for updating New Launch Campaign values
newlaunch_adj_criteria = [
    # format:
    # (min roas, max roas, budget adj, tCPA adj, status, maturity)
    (.70, .75, 0.0, 0.0, 'PAUSE'),
    (.75, 1, 0.0, .10, 'ACTIVE'),
    (1, 100, .25, 0.0, 'ACTIVE')
]


# temp columns to house new values and make sure all values are cleared out
TMP_STATUS = RPT_COL_CAMPAIGN_STATUS + '_'
inputDf[TMP_STATUS] = np.nan

TMP_BUDGET = RPT_COL_DAILY_BUDGET + '_'
inputDf[TMP_BUDGET] = np.nan

TMP_TARGETCPA = RPT_COL_PUBLISHER_TARGETCPA + '_'
inputDf[TMP_TARGETCPA] = np.nan


# loop through each adj criteria tuple and apply
for (min_roas, max_roas, budget_adj, tcpa_adj, status) in newlaunch_adj_criteria:

    print(f"Applying adj criteria: min roas={min_roas}, max roas={max_roas}, budget adj={budget_adj}, tcpa adj={tcpa_adj}, status ={status}")

    matched_campaigns = (inputDf[RPT_COL_ROAS] >= min_roas) & \
                        (inputDf[RPT_COL_ROAS] < max_roas)
                        

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))

        new_budget = inputDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] * (1 + budget_adj)
        inputDf.loc[ matched_campaigns, TMP_BUDGET ] = new_budget

        new_tcpa = inputDf.loc[matched_campaigns, RPT_COL_PUBLISHER_TARGETCPA] * (1 - tcpa_adj)
        inputDf.loc[ matched_campaigns, TMP_TARGETCPA ] = new_tcpa

        inputDf.loc[ matched_campaigns, TMP_STATUS] = status

        print("adj applied", tableize(inputDf.loc[matched_campaigns]))


# find changed campaigns
changed = (inputDf[TMP_BUDGET].notnull() & (inputDf[RPT_COL_DAILY_BUDGET] != inputDf[TMP_BUDGET])) | \
          (inputDf[TMP_TARGETCPA].notnull() & (inputDf[RPT_COL_PUBLISHER_TARGETCPA] != inputDf[TMP_TARGETCPA])) | \
          (inputDf[TMP_STATUS].notnull() & (inputDf[RPT_COL_CAMPAIGN_STATUS] != inputDf[TMP_STATUS])) 

if sum(changed) > 0:

    print("== Campaigns with Changed Adj ==", tableize(inputDf.loc[changed]))

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, TMP_STATUS]
    outputDf = inputDf.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA, \
                        TMP_STATUS: BULK_COL_STATUS \
                    })
else:
    
    print("Empty inputDf")
    outputDf = outputDf.iloc[0:0]

print("outputDf", tableize(outputDf))

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus