Script 153: Mature Campaigns Adj Google tCPA and Budget

Purpose

Adjust the tCPA and daily budget of Google campaigns labeled as “Mature” based on the ROAS over the previous 14 days.

To Elaborate

The Python script aims to automate the adjustment of tCPA (target cost per acquisition) and daily budget for Google campaigns that are labeled as “Mature”. The adjustments are based on the ROAS (return on ad spend) over the previous 14 days. The script applies different adjustment criteria depending on the ROAS range, and it takes into account whether the campaign has had previous adjustments or not. The goal is to optimize the campaign performance by aligning the tCPA and budget with the desired ROAS.

Walking Through the Code

  1. The script defines the criteria for campaign strategy target and daily budget adjustments.
  2. It assigns column parameters for the input data.
  3. The script creates temporary columns to store new values for the tCPA and budget adjustments.
  4. It checks if the “Date of Last tCPA / Daily Budget Adj.” column is blank.
  5. Based on the check, the script separates the campaigns into two dataframes: one for campaigns with no last updated value and one for campaigns with a last updated value.
  6. For campaigns with no last updated value, the script applies the adjustment criteria and stores the changed campaigns in a separate dataframe.
  7. For campaigns with a last updated value, the script calculates the days since the last update and applies the adjustment criteria only if the days since the last update is greater than 14.
  8. The script merges the dataframes of campaigns with no last updated value and campaigns with a last updated value.
  9. If the resulting output dataframe has tCPA values below 2.50, the script sets them to 2.50.
  10. The script prints the resulting output dataframe.

Vitals

  • Script ID : 153
  • Client ID / Customer ID: 1306925431 / 60269477
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Daily Budget, Publisher Target CPA, Date of Last tCPA / Daily Budget Adj.
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-05-30 20:38
  • Last Updated by alejandro@rainmakeradventures.com on 2024-01-26 18:56
> 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#
# Publisher Budget and Target CPA Adjustment - Mature Camapaigns
#
#
# Author: Byron Porter
# Date: 2023-05-30
#


# define criteria for campaign Strategy Target and Daily Budget adjustments
# note: MIN values are inclusive; MAX values are non-inclusive
campaign_adj_criteria = [
    # format:
    #  (min roas, max roas, tCPA adj, budget adj),
    (0.75, 1.00, -0.10, -0.25),
    (1.00, 1.25, 0.0, 0.25),
    (1.25, 1.50, 0.0, 0.5),
    (1.50, 2.00, 0.0, 0.75),
    (2.00, 999999.0, 0.0, 1.0)
]

campaign_adj_criteria_2 = [
    # format:
    #  (min roas, max roas, tCPA adj, budget adj),
    (0.50, 0.75, -0.20, 25.00)
]

campaign_adj_criteria_3 = [
    # format:
    #  (min roas, max roas, tCPA adj, budget adj),
    (0.25, 0.50, 2.50, 15.00)
]


# define column parameters
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_ROAS = 'CLICKS ROAS'
RPT_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
RPT_COL_CAMP_MATURITY = 'Campaign Maturity'
RPT_COL_LAST_TCPA_BUDGET_UPDATE = 'Date of Last tCPA / Daily Budget Adj.'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_DAILY_BUDGET = 'Daily Budget'
BULK_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'


# Assign current date to a parameter
today = datetime.datetime.now()

# create temp column to store new values and default to empty
TMP_BUDGET = RPT_COL_DAILY_BUDGET + '_'
inputDf[TMP_BUDGET] = np.nan
TMP_TARGETCPA = RPT_COL_PUBLISHER_TARGETCPA + '_'
inputDf[TMP_TARGETCPA] = np.nan

# Check if RPT_COL_LAST_TCPA_BUDGET_UPDATE is blank
null_check = inputDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE].isnull()

# Use check to create DataFrame for campaigns with no Last Updated value and assign current date
blankDf = inputDf.loc[null_check, :].copy()
blank2Df = inputDf.loc[null_check, :].copy()
blank3Df = inputDf.loc[null_check, :].copy()
nonblankDf = inputDf.loc[~null_check, :].copy()
nonblank2Df = inputDf.loc[~null_check, :].copy()
nonblank3Df = inputDf.loc[~null_check, :].copy()

nodateDf = pd.DataFrame()
nodate2Df = pd.DataFrame()
nodate3Df = pd.DataFrame()

# Define empty data frame for campaigns with changed RPT_COL_LAST_TCPA_BUDGET_UPDATE values
dateDf = pd.DataFrame()
date2Df = pd.DataFrame()
date3Df = pd.DataFrame()

############################# First Campaign Criteria Updates *No Previous Updates* ############################# 

# loop through each ROAS criteria and apply
for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria:

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

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

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        new_budget = blankDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] * (1 + budget_adj)
        # print("new_budget", new_budget)
        
        blankDf.loc[ matched_campaigns, TMP_BUDGET ] = new_budget

        new_tcpa = blankDf.loc[matched_campaigns, RPT_COL_PUBLISHER_TARGETCPA] * (1 + tcpa_adj)

        blankDf.loc[ matched_campaigns, TMP_TARGETCPA ] = new_tcpa

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    nodateDf = blankDf.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    nodateDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()
else:
    
    print("Empty nodateDf")
    nodateDf = nodateDf.iloc[0:0]



############################# Second Campaign Criteria Updates *No Previous Updates* ############################# 

# loop through other ORAS criteria and apply
for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria_2:

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

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

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        #new_budget = blankDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] - budget_adj
        # print("new_budget", new_budget)
        
        blank2Df.loc[ matched_campaigns, TMP_BUDGET ] = budget_adj

        new_tcpa = blank2Df.loc[matched_campaigns, RPT_COL_PUBLISHER_TARGETCPA] * (1 + tcpa_adj)

        blank2Df.loc[ matched_campaigns, TMP_TARGETCPA ] = new_tcpa

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    nodate2Df = blank2Df.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    nodate2Df[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()
else:
    
    print("Empty nodate2Df")
    nodate2Df = nodate2Df.iloc[0:0]


############################# Third Campaign Criteria Updates *No Previous Updates* ############################# 


for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria_3:

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

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

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        #new_budget = blankDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] - budget_adj
        # print("new_budget", new_budget)
        
        blank3Df.loc[ matched_campaigns, TMP_BUDGET ] = budget_adj

        blank3Df.loc[ matched_campaigns, TMP_TARGETCPA ] = tcpa_adj

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    nodate3Df = blank3Df.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    nodate3Df[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()
else:
    
    print("Empty nodate3Df")
    nodate3Df = nodate3Df.iloc[0:0]


############################# First Campaign Criteria Updates *Previous Updates* ############################# 

# create temp columm to store the days since last update
nonblankDf['ConvertedDate'] = pd.to_datetime(nonblankDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE], format="%Y-%m-%d")
nonblankDf['DaysSinceUpdate'] = (today - nonblankDf['ConvertedDate']).dt.days

nonblank2Df['ConvertedDate'] = pd.to_datetime(nonblankDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE], format="%Y-%m-%d")
nonblank2Df['DaysSinceUpdate'] = (today - nonblankDf['ConvertedDate']).dt.days

nonblank3Df['ConvertedDate'] = pd.to_datetime(nonblankDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE], format="%Y-%m-%d")
nonblank3Df['DaysSinceUpdate'] = (today - nonblankDf['ConvertedDate']).dt.days

# loop through each ROAS criteria, check for last update, and apply
for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria:

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

    matched_campaigns = (nonblankDf[RPT_COL_ROAS] >= min_roas) & \
                        (nonblankDf[RPT_COL_ROAS] < max_roas) & \
                        (nonblankDf['DaysSinceUpdate'] > 14)

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        new_budget = nonblankDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] * (1 + budget_adj)
        # print("new_budget", new_budget)
        
        nonblankDf.loc[ matched_campaigns, TMP_BUDGET ] = new_budget

        new_tcpa = nonblankDf.loc[matched_campaigns, RPT_COL_PUBLISHER_TARGETCPA] * (1 + tcpa_adj)

        nonblankDf.loc[ matched_campaigns, TMP_TARGETCPA ] = new_tcpa

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    dateDf = nonblankDf.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    dateDf[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()

else:
    
    print("Empty dateDf")
    dateDf = dateDf.iloc[0:0]


############################# Second Campaign Criteria Updates *Previous Updates* ############################# 

# loop through each ROAS other criteria, check for last update, and apply
for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria_2:

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

    matched_campaigns = (nonblank2Df[RPT_COL_ROAS] >= min_roas) & \
                        (nonblank2Df[RPT_COL_ROAS] < max_roas) & \
                        (nonblank2Df['DaysSinceUpdate'] > 14)

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        #new_budget = nonblank2Df.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] - budget_adj
        # print("new_budget", new_budget)
        
        nonblank2Df.loc[ matched_campaigns, TMP_BUDGET ] = budget_adj

        new_tcpa = nonblank2Df.loc[matched_campaigns, RPT_COL_PUBLISHER_TARGETCPA] * (1 + tcpa_adj)

        nonblank2Df.loc[ matched_campaigns, TMP_TARGETCPA ] = new_tcpa

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    date2Df = nonblank2Df.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    date2Df[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()

else:
    
    print("Empty date2Df")
    dateotherDf = date2Df.iloc[0:0]


############################# Third Campaign Criteria Updates *Previous Updates* ############################# 


for (min_roas, max_roas, tcpa_adj, budget_adj) in campaign_adj_criteria_3:

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

    matched_campaigns = (nonblank3Df[RPT_COL_ROAS] >= min_roas) & \
                        (nonblank3Df[RPT_COL_ROAS] < max_roas) & \
                        (nonblank2Df['DaysSinceUpdate'] > 14)
                        

    if sum(matched_campaigns) > 0:
        print("matched campaigns: ", sum(matched_campaigns))
        #new_budget = blankDf.loc[matched_campaigns, RPT_COL_DAILY_BUDGET] - budget_adj
        # print("new_budget", new_budget)
        
        nonblank3Df.loc[ matched_campaigns, TMP_BUDGET ] = budget_adj

        nonblank3Df.loc[ matched_campaigns, TMP_TARGETCPA ] = tcpa_adj

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


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

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_BUDGET, TMP_TARGETCPA, RPT_COL_LAST_TCPA_BUDGET_UPDATE]
    date3Df = blank3Df.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_BUDGET: BULK_COL_DAILY_BUDGET, \
                        TMP_TARGETCPA: BULK_COL_PUBLISHER_TARGETCPA \
                    })
    date3Df[RPT_COL_LAST_TCPA_BUDGET_UPDATE] = datetime.date.today()
else:
    
    print("Empty date3Df")
    date3Df = date3Df.iloc[0:0]



# Merge defined data, exlcuding those that are empty, and print the resulting outputDf
dataframes = [nodateDf, dateDf, nodate2Df, date2Df, nodate3Df, date3Df]
non_empty_dataframes = [df for df in dataframes if not df.empty]

if non_empty_dataframes:
    outputDf = pd.concat(non_empty_dataframes)
    
else:
    print("Empty outputDf")
    outputDf = outputDf.iloc[0:0]

if (outputDf[BULK_COL_PUBLISHER_TARGETCPA] < 2.50).any():
    outputDf.loc[outputDf[BULK_COL_PUBLISHER_TARGETCPA] < 2.50, BULK_COL_PUBLISHER_TARGETCPA] = 2.50
    print("outputDf:", tableize(outputDf))
else:
    print("outputDf", tableize(outputDf))

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

comments powered by Disqus