Script 139: Increase Campaign Daily Budget

Purpose

Increase daily budget by a certain percentage for campaigns that have an impression share greater than a certain percentage over the last few days.

To Elaborate

The Python script increases the daily budget for campaigns that meet certain criteria. The criteria include having an impression share greater than a specified percentage and having a last updated date for the daily budget that is more than 14 days ago. The script identifies campaigns that meet these criteria and updates their daily budget by a specified percentage. The script also handles cases where the last updated date is missing for a campaign, assigning the current date as the last updated date.

Walking Through the Code

  1. Define column constants for the input and output dataframes.
  2. Assign the current date to the “today” variable.
  3. Check if the “Last Updated - Daily Budget” column is blank in the input dataframe.
  4. Create a dataframe (“blankDf”) for campaigns with no last updated value and assign the current date as the last updated date.
  5. Create a dataframe (“nonblankDf”) for campaigns with a last updated value.
  6. Convert the date string in the “Last Updated - Daily Budget” column to a date object in the “nonblankDf” dataframe.
  7. Create a temporary column to calculate the number of days since the last update.
  8. Create a dataframe (“dateDf”) for campaigns that have not been updated in the last 14 days.
  9. Assign the current date as the last updated date for campaigns in the “dateDf” dataframe.
  10. Merge the “blankDf” and “dateDf” dataframes to create the output dataframe.
  11. Print the output dataframe in a tabular format.

Vitals

  • Script ID : 139
  • Client ID / Customer ID: 1306924501 / 60269325
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Daily Budget, Last Updated - Daily Budget
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2023-05-25 18:31
  • 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
#
# Increase Campaign Daily Budget
#
# Byron Porter
# 2023-05-26
#


RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_LOST_IMPRSHAREBUDGET = 'Lost Impr. Share (Budget) %'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_LAST_UPDATEDDAILYBUDGET = 'Last Updated - Daily Budget'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_DAILY_BUDGET = 'Daily Budget'
BULK_COL_LAST_UPDATEDDAILYBUDGET = 'Last Updated - Daily Budget'

########## CONFIGURABLE PARAMS - END ########## 

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

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

# Use check to create DataFrame for campaigns with no Last Updated value and assign current date
blankDf = inputDf.loc[null_check, [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_DAILY_BUDGET, RPT_COL_LAST_UPDATEDDAILYBUDGET]]
blankDf.loc[:, RPT_COL_LAST_UPDATEDDAILYBUDGET] = today.date()  # today

# Use check to create DataFrame for campaigns with a Last Updated value
nonblankDf = inputDf.loc[~null_check, [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_DAILY_BUDGET, RPT_COL_LAST_UPDATEDDAILYBUDGET]]

# Convert date string in RPT_COL_LAST_UPDATEDDAILYBUDGET column to date object
nonblankDf['ConvertedDate'] = pd.to_datetime(inputDf[RPT_COL_LAST_UPDATEDDAILYBUDGET], format="%Y-%m-%d")

# Create temp column to show number of days since last update
nonblankDf['DaysSinceUpdate'] = (today - nonblankDf['ConvertedDate']).dt.days

# Use check to create a DataFrame of campaigns to update
diff_check = nonblankDf['DaysSinceUpdate'] > 14

dateDf = nonblankDf.loc[diff_check, [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_DAILY_BUDGET, RPT_COL_LAST_UPDATEDDAILYBUDGET]]
dateDf.loc[:, RPT_COL_LAST_UPDATEDDAILYBUDGET] = today.date()

# Merge blankDf and dateDf to outputDf
#outputDf = pd.concat([blankDf, dateDf])
outputDf = blankDf.merge(dateDf, how='outer')

print(tableize(outputDf))

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

comments powered by Disqus