Script 1759: Site Down
Purpose:
The Python script processes campaign data to update the status and paused date based on specific conditions.
To Elaborate
The Python script is designed to manage and update the status of marketing campaigns based on their current state and a specific condition related to a “Site Down” event. It processes data from a primary source, checks the status of each campaign, and updates it accordingly. If a campaign is currently “Paused” and has a recorded “Site Down Paused Date,” it changes the status to “Active” and clears the paused date. Conversely, if a campaign is “Active,” it changes the status to “Paused” and records the current date as the “Site Down Paused Date.” This logic ensures that campaigns are appropriately managed in response to site availability issues, maintaining accurate records of their operational status.
Walking Through the Code
- Data Initialization
- The script begins by importing necessary libraries,
datetime
andpandas
, for date manipulation and data handling. - It retrieves the primary data source into a DataFrame named
inputDf
and defines several column constants for easy reference.
- The script begins by importing necessary libraries,
- Output DataFrame Setup
- An output DataFrame,
outputDf
, is initialized by copying relevant columns frominputDf
. - A new column,
BULK_COL_STATUS
, is created inoutputDf
and initialized with values from the campaign status column.
- An output DataFrame,
- Status Update Logic
- A function
update_status
is defined to determine the new status and paused date for each campaign. - The function checks if a campaign is “Paused” with a non-null paused date, updating it to “Active” and clearing the date.
- If a campaign is “Active,” it updates the status to “Paused” and sets the current date as the paused date.
- A function
- Applying the Logic
- The
apply
method is used to executeupdate_status
on each row ofoutputDf
, updating the status and paused date columns accordingly.
- The
- Output
- The script concludes by printing the first few rows of the updated
outputDf
to display the changes made.
- The script concludes by printing the first few rows of the updated
Vitals
- Script ID : 1759
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, Status, Site Down Paused Date, Campaign ID
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
- Created by ascott@marinsoftware.com on 2025-02-21 21:34
- Last Updated by Michael Huang on 2025-02-28 00:42
> 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
## name: Site Down
## description:
##
##
## author:
## created: 2025-02-21
##
import datetime
import pandas as pd
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_PROGRAM = 'Program'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_SITE_DOWN_PAUSED_DATE = 'Site Down Paused Date'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STATUS = 'Status'
BULK_COL_SITE_DOWN_PAUSED_DATE = 'Site Down Paused Date'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
# Initialize the output dataframe
outputDf = inputDf[[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_CAMPAIGN_ID, RPT_COL_CAMPAIGN_STATUS, RPT_COL_SITE_DOWN_PAUSED_DATE]].copy()
# Initialize the BULK_COL_STATUS column with the values from RPT_COL_CAMPAIGN_STATUS
outputDf[BULK_COL_STATUS] = outputDf[RPT_COL_CAMPAIGN_STATUS]
# Apply logic to update BULK_COL_STATUS and BULK_COL_SITE_DOWN_PAUSED_DATE
def update_status(row):
if row[BULK_COL_STATUS] == "Paused":
if pd.notna(row[RPT_COL_SITE_DOWN_PAUSED_DATE]):
return "Active", pd.NaT
if row[RPT_COL_CAMPAIGN_STATUS] == "Active":
return "Paused", today
else:
return row[RPT_COL_CAMPAIGN_STATUS], row[RPT_COL_SITE_DOWN_PAUSED_DATE]
outputDf[[BULK_COL_STATUS, BULK_COL_SITE_DOWN_PAUSED_DATE]] = outputDf.apply(
lambda row: pd.Series(update_status(row)), axis=1
)
# user code start here
print(outputDf.head())
Post generated on 2025-03-11 01:25:51 GMT