Script 149: Pause Keywords Automation
Purpose
Pause Keyword and label the Dimension ‘AutoPause’ with today’s date according to Creation Date, Accumulated Clicks, Accumulated Spend.
To Elaborate
The Python script solves the problem of pausing keywords and labeling them with the dimension ‘AutoPause’ based on certain criteria. The criteria include the creation date of the keyword, the accumulated clicks, and the accumulated spend. If a keyword meets the specified criteria, it will be paused and labeled with today’s date.
Walking Through the Code
- The script defines various column constants for the input and output dataframes.
- The script sets initial values for the ‘Status’ and ‘AutoPause’ columns in the output dataframe.
- The script defines the dates to check, including today’s date and a date 60 days ago.
- The script defines the new status of the keyword as “Paused”.
- The script defines the value for the ‘AutoPause’ dimension as today’s date.
- The script creates temporary columns for the new status and the ‘AutoPause’ dimension in the input dataframe.
- The script converts the ‘Creation Date’ column into a date type.
- The script applies a rule to identify keywords that meet the criteria for pausing and labeling.
- The script updates the temporary columns with the new status and ‘AutoPause’ dimension for the matched keywords.
- The script identifies the keywords that have changed status.
- If there are changed keywords, the script selects the relevant columns and creates the output dataframe.
- If there are no changed keywords, the script prepares an empty output dataframe.
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-05-15 07:44:05 GMT