Script 1337: Script Auto Pause After Event
Purpose
The script automatically pauses campaigns in a dataset if their associated event dates have passed.
To Elaborate
The Python script is designed to manage marketing campaigns by automatically updating their status based on event dates. It processes a dataset containing campaign information, including event dates, and checks if these dates have passed. If an event date is earlier than the current date, the script changes the campaign’s status to “Paused.” This ensures that outdated campaigns are not active, helping to maintain an accurate and up-to-date campaign schedule. The script is particularly useful for marketing teams who need to manage large volumes of campaigns efficiently, ensuring that only relevant and timely campaigns remain active.
Walking Through the Code
- Data Preparation
- The script begins by extracting the date from a specific column in the dataset, converting it into a datetime object for comparison purposes. This is crucial for determining whether the campaign’s event date has passed.
- Status Initialization
- A new column for status is initialized with the current group status. This sets a baseline for the status of each campaign before any changes are made.
- Status Update
- The script uses vectorized operations to update the status of campaigns. If the parsed event date is earlier than the current date, the status is set to “Paused.” This operation is efficient and handles large datasets effectively.
- Output Preparation
- The script prepares an output DataFrame containing specific columns, ensuring that only relevant information is included. All statuses in this output are set to “Paused,” reflecting the updates made.
- Result Display
- Finally, the script prints the resulting DataFrame, providing a clear view of the campaigns that have been paused. This output can be used for further analysis or reporting.
Vitals
- Script ID : 1337
- Client ID / Customer ID: 1306928223 / 60270455
- Action Type: Bulk Upload (Preview)
- Item Changed: AdGroup
- Output Columns: Account, Campaign, Group, Status
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
- Created by Grégory Pantaine on 2024-08-16 13:47
- Last Updated by Grégory Pantaine on 2024-08-16 14:11
> 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
RPT_COL_GROUP = 'Group'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_GROUP_STATUS = 'Group Status'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_STATUS = 'Status'
# Assuming inputDf is the DataFrame where the data is stored
# Get the current date and time in UTC without timezone information
today = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
# Extract the date from RPT_COL_GROUP and convert it to a datetime object
inputDf['ParsedDate'] = inputDf[RPT_COL_GROUP].str.extract(r'(\d{1,2}/\d{1,2}/\d{4})')
inputDf['ParsedDate'] = pd.to_datetime(inputDf['ParsedDate'], format="%mm.%dd.%YYYY")
# Initialize new Status column with the current Group Status
inputDf[BULK_COL_STATUS] = inputDf[RPT_COL_GROUP_STATUS]
# Update the Status column using vectorized operations
inputDf.loc[inputDf['ParsedDate'] < today, BULK_COL_STATUS] = "Paused"
# Check for changes
####changed = (inputDf[BULK_COL_STATUS] == "Paused") & (inputDf[BULK_COL_STATUS] != inputDf[RPT_COL_GROUP_STATUS])
####if changed.sum() > 0:
#### print("status changed", tableize(inputDf[changed]))
# Filter and output the modified rows with specific columns
###outputDf = inputDf[changed][[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, BULK_COL_STATUS]]
# Select the specific columns from inputDf to create outputDf
outputDf = inputDf[[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, BULK_COL_STATUS]]
# Set all values in the BULK_COL_STATUS column of outputDf to "Paused"
outputDf[BULK_COL_STATUS] = "Paused"
# Print the resulting output DataFrame
print("outputDf", tableize(outputDf))
###else:
### print("No changes")
### outputDf = pd.DataFrame(columns=[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, BULK_COL_STATUS])
Post generated on 2024-11-27 06:58:46 GMT