Script 117: Strategy Assignment
Purpose
The Python script assigns a marketing strategy to campaigns based on their name, creation date, and accumulated clicks.
To Elaborate
The script is designed to automate the assignment of marketing strategies to campaigns by evaluating specific criteria. It examines the campaign name for certain tokens, the creation date of the campaign, and the number of accumulated clicks. Based on these factors, it assigns one of several predefined strategies. The script ensures that campaigns are categorized correctly, which helps in managing and optimizing marketing efforts. The business rules include assigning specific strategies if the campaign name contains certain tokens, if the campaign was created more than 14 days ago with a minimum number of clicks, or if the campaign was created more than 60 days ago. This structured approach helps in maintaining consistency and efficiency in campaign management.
Walking Through the Code
- Initialization
- The script begins by defining constants for column names and strategies. These constants are used throughout the script to reference specific data fields and strategy values.
- It initializes the strategy column in the output DataFrame with a placeholder value.
- Strategy Definition
- Several strategy values are defined, such as “SVOD - SG - Title_Exc-Launch - TIS” and “SVOD - SG - Title-Evergreen - CPS”. These represent different marketing strategies that can be assigned to campaigns.
- Campaign Token and Date Definitions
- Tokens like “_ Exc” and “_ NonExc” are defined to identify specific types of campaigns based on their names.
- Dates are calculated to determine if a campaign was created more than 14 or 60 days ago, which are used in subsequent rules.
- Rule Application
- Rule 1: Campaigns with names containing specific tokens are assigned corresponding strategies. This is done using case-insensitive string matching.
- Rule 2: Campaigns created more than 14 days ago with at least 100 clicks are reassigned to a “CPA BAU” strategy.
- Rule 3: Campaigns older than 60 days are assigned to a “CPA Evergreen” strategy.
- Change Detection and Output Preparation
- The script identifies campaigns where the strategy has changed and prepares an output DataFrame containing only these campaigns.
- If no changes are detected, an empty DataFrame is prepared.
Vitals
- Script ID : 117
- Client ID / Customer ID: 1306923691 / 69058
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Strategy
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeremy Brown (jbrown@marinsoftware.com)
- Created by Jeremy Brown on 2023-05-19 11:11
- Last Updated by Jonathan Reichl on 2023-12-13 11:20
> 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#
# Assign Campaign to Strategy according to:
# - Campaign Name
# - Campaign Creation Date
# - accumulated clicks
#
# Author: Michael S. Huang
# Date: 2023-05-18
#
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_CREATIONDATE = 'Campaign Creation Date'
RPT_COL_CLICKS = 'Clicks'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STRATEGY = 'Strategy'
outputDf[BULK_COL_STRATEGY] = "<<YOUR VALUE>>"
# define Strategies to map to
VAL_STRATEGY_IS_EXC = "SVOD - SG - Title_Exc-Launch - TIS"
VAL_STRATEGY_IS_NON_EXC = "SVOD - SG - Title_NonExc-Launch - TIS"
VAL_STRATEGY_CPA_BAU = "SVOD - SG - Title-BAU - CPS"
VAL_STRATEGY_CPA_EVERGREEN = "SVOD - SG - Title-Evergreen - CPS"
# define campaign tokens to match
VAL_CAMPAIGN_NAME_TOKEN_EXC = "_ Exc"
VAL_CAMPAIGN_NAME_TOKEN_NON_EXC = "_ NonExc"
# defines dates to check
print("timezone", CLIENT_TIMEZONE)
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
date_14_days_ago = pd.to_datetime(today - datetime.timedelta(days=14))
date_60_days_ago = pd.to_datetime(today - datetime.timedelta(days=60))
print(today, date_14_days_ago, date_60_days_ago)
# define tmp column for new Strategy and set to empty
TMP_STRATEGY = RPT_COL_STRATEGY + '_'
inputDf[TMP_STRATEGY] = np.nan
### Rule 1: Assign to Exc/NonExc Strategy based on token in campaign name
### case insensitive
inputDf.loc[ inputDf[RPT_COL_CAMPAIGN].str.contains(VAL_CAMPAIGN_NAME_TOKEN_EXC, case=False), \
TMP_STRATEGY \
] = VAL_STRATEGY_IS_EXC
inputDf.loc[ inputDf[RPT_COL_CAMPAIGN].str.contains(VAL_CAMPAIGN_NAME_TOKEN_NON_EXC, case=False), \
TMP_STRATEGY \
] = VAL_STRATEGY_IS_NON_EXC
### Rule 2: if Exc/NonExc campaign created more than 14 days ago and clicks >= 100,
### assign to CPA BAU
inputDf.loc[ ( inputDf[RPT_COL_CAMPAIGN].str.contains(VAL_CAMPAIGN_NAME_TOKEN_EXC, case=False) | \
inputDf[RPT_COL_CAMPAIGN].str.contains(VAL_CAMPAIGN_NAME_TOKEN_NON_EXC, case=False) ) & \
(inputDf[RPT_COL_CAMPAIGN_CREATIONDATE] <= date_14_days_ago) & \
(inputDf[RPT_COL_CLICKS] >= 100), \
TMP_STRATEGY \
] = VAL_STRATEGY_CPA_BAU
### Rule 3: if campaign created more than 60 days ago,
### assign to CPA Evergreen
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN_CREATIONDATE] <= date_60_days_ago), \
TMP_STRATEGY \
] = VAL_STRATEGY_CPA_EVERGREEN
# find changed campaigns
changed = inputDf[TMP_STRATEGY].notnull() & (inputDf[RPT_COL_STRATEGY] != inputDf[TMP_STRATEGY])
# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
print("== Campaigns with Changed Strategy ==", tableize(inputDf.loc[changed]))
# only select changed rows
cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, TMP_STRATEGY]
outputDf = inputDf.loc[ changed, cols ].copy() \
.rename(columns = { \
TMP_STRATEGY: BULK_COL_STRATEGY \
})
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
outputDf = outputDf.iloc[0:0]
Post generated on 2024-11-27 06:58:46 GMT