Script 289: Google Campaign tROAS Adjustment

Purpose

Python script solves the problem of adjusting the Publisher Target ROAS for Google campaigns based on the daily spend goal and publisher cost.

To Elaborate

The Python script aims to adjust the Publisher Target ROAS for Google campaigns based on the daily spend goal and publisher cost. The adjustment is made to optimize the campaign’s performance and ensure that the target ROAS aligns with the actual cost and spend.

The key business rules involved in this problem are:

  • If the publisher cost is greater than 160, the adjustment should increase the target ROAS by 20%.
  • If the publisher cost is less than or equal to 160, the adjustment should decrease the target ROAS by 20%.

The script takes the input data, calculates the adjustment using the defined rules, and outputs the adjusted target ROAS for each campaign.

Walking Through the Code

  1. The script defines the column constants used in the input and output dataframes.
  2. The current date is assigned to the “today” parameter.
  3. A temporary column is created in the input dataframe to store the calculated tROAS adjustment.
  4. The “calculate_TMP_TROASADJ” function is defined to calculate the adjustment based on the publisher cost and target ROAS.
  5. The function checks if the publisher cost is greater than 160 and applies the corresponding adjustment.
  6. The function returns the adjusted target ROAS or None if there is a non-numeric value.
  7. The function is applied to the input dataframe to populate the temporary column.
  8. The adjusted target ROAS is printed using the “tableize” function.
  9. The columns to be copied to the output dataframe are defined.
  10. The input dataframe is copied to the output dataframe, and the temporary column is renamed for bulk upload.
  11. The output dataframe with the selected columns is printed using the “tableize” function.

Vitals

  • Script ID : 289
  • Client ID / Customer ID: 1306925293 / 60269377
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Publisher Target ROAS
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-09-11 19:37
  • Last Updated by Byron Porter 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
#
# Publisher Target CPA Adjustment - Intraday
#
#
# Author: Byron Porter
# Date: 2023-09-25
#


# define criteria for intraday campaign tCPA and Daily Budget adjustment
# note: MIN values are inclusive; MAX values are non-inclusive

RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_DAILY_SPEND_GOAL = 'Daily Spend Goal'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_PUB_TARGET_ROAS = 'Publisher Target ROAS'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PUB_TARGET_ROAS = 'Publisher Target TROAS'



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

# create temp column to store calculated tROAS adjustment, and default to empty
TMP_TROASADJ = RPT_COL_PUB_TARGET_ROAS + '_'
inputDf[TMP_TROASADJ] = np.nan

#calculate the desktop device modifier adjustment. checks if cost/set lead is greater than the goal of 1500 to determine if the adjustment should be lowered or increased

def calculate_TMP_TROASADJ(row):
    try:
        target_roas = float(row[RPT_COL_PUB_TARGET_ROAS])
        if row[RPT_COL_PUB_COST] > 160:  # You might want to use RPT_COL_DAILY_SPEND_GOAL here
            return (target_roas + target_roas * 0.20) * 100
        else:
            return (target_roas - target_roas * 0.20) * 100
    except ValueError:
        return None  # Handle non-numeric values gracefully
        
# Apply the function to create TMP_ROASADJ
inputDf[TMP_TROASADJ] = inputDf.apply(calculate_TMP_TROASADJ, axis=1)

print("== Campaign Publisher tROAS After Adjustment ==", tableize(inputDf))

# Define which columns to copy over to output Dataframe
cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_TROASADJ]

# copy inputDf to outputDf and rename the temp column for bulk upload
outputDf = inputDf.loc[:, cols].copy() \
                    .rename(columns = { \
                        TMP_TROASADJ : "Publisher Target ROAS"
                    })

# Print the DataFrame with the selected columns
print(tableize(outputDf))

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

comments powered by Disqus