Script 163: Mature Campaigns Pause Low ROAS

Purpose

Pause mature campaigns with low ROAS based on specific criteria.

To Elaborate

The Python script aims to identify mature campaigns with low ROAS (Return on Ad Spend) and pause them. The script defines specific criteria for campaign maturity and ROAS values to determine which campaigns should be paused. It then applies these criteria to the input data and generates an output dataframe with the campaigns that have been paused. The script also includes a check for any campaigns that have changed status and outputs a separate dataframe for those campaigns.

Walking Through the Code

  1. The script defines the campaign pause criteria, including the maturity level and the minimum and maximum ROAS values.
  2. It defines column parameters for the input and output dataframes.
  3. A temporary column is created in the input dataframe to store the new status of the campaigns.
  4. The script loops through each pause criteria and applies it to the input dataframe, updating the temporary status column for the campaigns that meet the criteria.
  5. It checks for any campaigns that have changed status by comparing the original campaign status column with the temporary status column.
  6. If there are changed campaigns, it prints a table of the campaigns with changed status and creates an output dataframe with the relevant columns.
  7. The output dataframe includes the account, campaign, new status, and the date the script was run.
  8. If there are no changed campaigns, it prints a message indicating an empty output dataframe.

Vitals

  • Script ID : 163
  • Client ID / Customer ID: 1306925431 / 60269477
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Script Paused, Status
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-06-06 22:04
  • Last Updated by alejandro@rainmakeradventures.com on 2024-01-26 18:55
> 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
#
# Pause Low ROAS Mature Campaigns
#
#
# Author: Byron Porter
# Date: 2023-06-06
#


# define campaign LOW ROAS PAUSE criteria
# note: MIN values are inclusive; MAX values are non-inclusive
campaign_pause_criteria = [
    # format:
    #  (mature, min roas, max roas),
    ('Mature', 0, .25)
    #('Mature II', 0, 1.2)
]


# define column parameters
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_CAMP_MATURITY = 'Campaign Maturity'
RPT_COL_ROAS = 'CLICKS ROAS'
RPT_COL_PUBLISHER_TARGETCPA = 'Publisher Target CPA'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STATUS = 'Status'


VAL_STATUS_PAUSED = 'Paused'

# create temp column to store new status and default to empty
TMP_STATUS = RPT_COL_CAMPAIGN_STATUS + '_'
inputDf[TMP_STATUS] = np.nan


# loop through each PAUSE criteria and apply
for (mature, min_roas, max_roas) in campaign_pause_criteria:
	print(f"Applying pause criteria: maturity={mature}, min roas={min_roas}, max roas={max_roas}")

	inputDf.loc[ (inputDf[RPT_COL_CAMP_MATURITY] == mature) & \
				 (inputDf[RPT_COL_ROAS] >= min_roas) & \
				 (inputDf[RPT_COL_ROAS] < max_roas), \
				 TMP_STATUS \
			   ] = VAL_STATUS_PAUSED

# find changed campaigns
changed = inputDf[TMP_STATUS].notnull() & (inputDf[RPT_COL_CAMPAIGN_STATUS] != inputDf[TMP_STATUS])

if sum(changed) > 0:

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

    # only select changed rows
    cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_STATUS]
    outputDf = inputDf.loc[ changed, cols ].copy() \
                    .rename(columns = { \
                        TMP_STATUS: BULK_COL_STATUS \
                    })

    outputDf['Script Paused'] = datetime.datetime.now().date()

    print("outputDf", tableize(outputDf))
    
else:
    
  print("Empty outputDf")
  outputDf = pd.DataFrame(columns=[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, BULK_COL_STATUS, 'Script Paused'])

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

comments powered by Disqus