Script 361: New Launch Updates Round 3

Purpose

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

To Elaborate

This Python script solves the problem of updating various parameters for new launch campaigns. The script applies different adjustment criteria based on the campaign’s ROAS (Return on Ad Spend) and maturity level. It updates the campaign’s status, daily budget, target CPA (Cost Per Acquisition), and maturity based on the defined criteria. The script also identifies campaigns that have been changed and outputs the updated values in a separate dataframe.

Walking Through the Code

  1. The script defines column constants for various campaign attributes.
  2. It sets the current maturity parameter value based on the input dataframe.
  3. The script defines the thresholds/conditions for updating new launch campaign values using a list of tuples.
    • Each tuple contains the minimum ROAS, maximum ROAS, budget adjustment, tCPA adjustment, status, and maturity.
  4. Temporary columns are created in the input dataframe to store the new values and ensure all previous values are cleared out.
  5. The script loops through each adjustment criteria tuple and applies the updates to the matched campaigns.
    • It checks if the campaign’s ROAS falls within the defined range.
    • If there are matched campaigns, it calculates the new budget and tCPA values based on the adjustments.
    • It updates the status, maturity, budget, and tCPA columns in the input dataframe.
  6. The script identifies campaigns that have been changed by comparing the original values with the updated values in the temporary columns.
  7. If there are changed campaigns, the script creates an output dataframe with the relevant columns and renames them accordingly.
  8. The script prints the output dataframe, which contains the campaigns with changed adjustments. If there are no changed campaigns, it prints an empty dataframe.

Vitals

  • Script ID : 361
  • Client ID / Customer ID: 1306925431 / 60269477
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Daily Budget, Publisher Target CPA, Status, Campaign Maturity
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-10-16 22:16
  • 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# 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', current_maturity),
    (.75, 1, 0.0, .10, 'ACTIVE', 'Mature'),
    (1, 100, .25, 0.0, 'ACTIVE', 'Mature')
]


# 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_PUB_STRATEGY = RPT_COL_PUBLISHER_BIDSTRATEGY + '_'
#inputDf[TMP_PUB_STRATEGY] = np.nan

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

TMP_MATURITY = RPT_COL_MATURITY + '_'
inputDf[TMP_MATURITY] = np.nan

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


# Convert the 'Date' column to datetime format
#inputDf['Date'] = pd.to_datetime(inputDf['Date'], format='%m/%d/%y')


# loop through each adj criteria tuple and apply
for (min_roas, max_roas, budget_adj, tcpa_adj, status, maturity) 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}, maturity={maturity}")

    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

        inputDf.loc[ matched_campaigns, TMP_MATURITY] = maturity

        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])) | \
          (inputDf[TMP_MATURITY].notnull() & (inputDf[RPT_COL_MATURITY] != inputDf[TMP_MATURITY]))

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, TMP_MATURITY]
    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, \
                        TMP_MATURITY: BULK_COL_MATURITY \
                    })
    #nodateDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()
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