Script 149: Pause Keywords Automation
Purpose
The script automates the process of pausing keywords and labeling them with the ‘AutoPause’ dimension based on specific criteria related to creation date, accumulated clicks, and spend.
To Elaborate
The Python script is designed to automate the management of keywords in a digital advertising context by pausing certain keywords and labeling them with the ‘AutoPause’ dimension. The script evaluates keywords based on three criteria: the creation date, accumulated clicks, and accumulated spend. If a keyword was created more than 60 days ago, has accumulated at least 100 clicks, and has a spend of at least 200 SGD, it is marked for pausing. The script then updates the status of these keywords to ‘Paused’ and labels them with the current date in the ‘AutoPause’ dimension. This process helps in managing the budget and performance of advertising campaigns by automatically identifying and pausing underperforming or costly keywords.
Walking Through the Code
- Initialization and Setup
- The script begins by defining constants for various column names used in the input and output data frames.
- It sets up the output data frame with placeholders for status and ‘AutoPause’ dimension values.
- Date Calculation
- The script calculates the current date and the date 60 days prior, which are used to evaluate the age of keywords.
- Temporary Columns for Status and AutoPause
- Temporary columns are created in the input data frame to store the new status and ‘AutoPause’ values for keywords that meet the criteria.
- Keyword Evaluation
- The script converts the ‘Creation Date’ column to a date format.
- It applies a rule to identify keywords that were created more than 60 days ago, have at least 100 clicks, and have a spend of at least 200 SGD.
- Keywords meeting these criteria are marked with a ‘Paused’ status and labeled with the current date in the ‘AutoPause’ dimension.
- Output Preparation
- The script identifies keywords with changed statuses and prepares an output data frame containing only these keywords.
- If no keywords meet the criteria, an empty output data frame is prepared.
Vitals
- Script ID : 149
- Client ID / Customer ID: 1306924343 / 69058
- Action Type: Bulk Upload (Preview)
- Item Changed: Keyword
- Output Columns: Account, Campaign, Group, Keyword, Match Type, Status, AutoPause
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeremy Brown (jbrown@marinsoftware.com)
- Created by Jeremy Brown on 2023-05-30 10:25
- Last Updated by Michael Huang 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
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
91
92
#
# Pause Keyword and label ONLY paused keywords with the Dimension 'AutoPause' with todays date according to:
# - Creation Date
# - Accumulated Clicks
# - Accumulated Spend
#
# Author: Jeremy Brown
# Date: 2023-05-30
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_MATCHTYPE = 'Match Type'
RPT_COL_GROUP = 'Group'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STATUS = 'Status'
RPT_COL_DIM_AUTOPAUSE = 'AutoPause'
RPT_COL_CREATIONDATE = 'Creation Date'
RPT_COL_CLICKS = 'Clicks'
RPT_COL_SPEND = 'Pub. Cost SGD'
RPT_COL_CONVERSIONS = 'Conv.'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_KEYWORD = 'Keyword'
BULK_COL_MATCHTYPE = 'Match Type'
BULK_COL_STATUS = 'Status'
BULK_COL_DIM_AUTOPAUSE = 'AutoPause'
outputDf[BULK_COL_STATUS] = "<<YOUR VALUE>>"
outputDf[BULK_COL_DIM_AUTOPAUSE] = "<<YOUR VALUE>>"
# defines dates to check
print("timezone", CLIENT_TIMEZONE)
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
date_60_days_ago = pd.to_datetime(today - datetime.timedelta(days=60))
print(today, date_60_days_ago)
# define new status of keyword
VAL_STATUS_MATCH = "Paused"
# define 'AutoPause' Dimension value to set (todays date)
VAL_DIMENSION_AUTOPAUSE = today
# define tmp column for new Status and set to empty
TMP_STATUS = RPT_COL_STATUS + '_'
inputDf[TMP_STATUS] = np.nan
# define tmp column for dimension 'AutoPause' and set to empty
TMP_AUTOPAUSE = RPT_COL_DIM_AUTOPAUSE + '_'
inputDf[TMP_AUTOPAUSE] = np.nan
# convert Creation Date into Date type
# example: 12/24/2022 5:04 PM
inputDf.loc[:, RPT_COL_CREATIONDATE] = pd.to_datetime(inputDf[RPT_COL_CREATIONDATE], \
format='%m/%d/%Y %H:%M %p')
### Rule: if keyword created more than 60 days ago AND clicks >= 100 AND spend >= 200,
### pause keyword and label dimension 'AutoPause' with todays date
matched_keywords = ( inputDf[RPT_COL_CREATIONDATE] <= date_60_days_ago) & \
(inputDf[RPT_COL_SPEND] >= 200) & \
(inputDf[RPT_COL_CLICKS] >= 100)
inputDf.loc[ matched_keywords, \
TMP_STATUS \
] = VAL_STATUS_MATCH
inputDf.loc[ matched_keywords, \
TMP_AUTOPAUSE \
] = VAL_DIMENSION_AUTOPAUSE
# find changed campaigns
changed = inputDf[TMP_STATUS].notnull() & (inputDf[RPT_COL_STATUS] != inputDf[TMP_STATUS])
# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
print("== Keywords with Changed Status ==", tableize(inputDf.loc[changed]))
# only select changed rows
cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, RPT_COL_KEYWORD, RPT_COL_MATCHTYPE, TMP_AUTOPAUSE, TMP_STATUS]
outputDf = inputDf.loc[ changed, cols ].copy() \
.rename(columns = { \
TMP_STATUS: BULK_COL_STATUS, \
TMP_AUTOPAUSE: BULK_COL_DIM_AUTOPAUSE \
})
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
outputDf = outputDf.iloc[0:0]
Post generated on 2024-11-27 06:58:46 GMT