Script 827: Cross Negate Single Keyword Campaigns
Purpose
The Python script generates negative keywords for each account in a dataset to optimize single keyword campaigns.
To Elaborate
The script addresses the problem of optimizing single keyword campaigns by generating negative keywords for each account in a dataset. In digital advertising, negative keywords are used to prevent ads from being triggered by certain search terms, thus refining targeting and improving campaign efficiency. The script processes a dataset containing information about keywords, accounts, campaigns, and groups. For each account, it identifies keywords that should not trigger ads for other keywords within the same account. This ensures that each keyword in a campaign is distinct and does not overlap with others, thereby preventing internal competition and improving the overall performance of the advertising campaigns.
Walking Through the Code
-
Function Definition: The script defines a function
generate_negative_keywords
that takes a DataFrameinput_df
as input. This function is responsible for generating negative keywords for each account in the dataset. -
Grouping Data: The data is grouped by the ‘Account’ column using the
groupby
method. This allows the script to process each account separately. -
Iterating Over Groups: For each account group, the script initializes a list to store negative keywords specific to that account. It then iterates over each row within the group to process individual keywords.
-
Identifying Negative Keywords: For each keyword in the account, the script identifies all other keywords within the same account that should be treated as negative keywords. It excludes the current keyword from this list to ensure that it does not negate itself.
-
Storing Negative Keywords: The identified negative keywords are stored in a dictionary format, including details such as the account, campaign, group, and match type. These dictionaries are appended to a list for the account.
-
Compiling Results: After processing all accounts, the script compiles a complete list of negative keywords for all accounts and returns this list.
-
Output: The script iterates over the compiled list of negative keywords and prints each one, detailing the negative keyword, account, campaign, group, and match type.
Vitals
- Script ID : 827
- Client ID / Customer ID: 1306922797 / 60269073
- Action Type: Bulk Upload (Preview)
- Item Changed: Keyword
- Output Columns: Account, Campaign, Group, Keyword, Match Type, Status
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
- Created by Grégory Pantaine on 2024-03-20 12:07
- Last Updated by Grégory Pantaine on 2024-03-20 12:16
> 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
# Define report column names
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_STATUS = 'Status'
RPT_COL_MATCH_TYPE = 'Match Type'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_GROUP = 'Group'
# Function to generate negative keywords for each account
def generate_negative_keywords(input_df):
# Initialize an empty list to store negative keywords for each account
negative_keywords_by_account = []
# Group data by account
grouped_data = input_df.groupby(RPT_COL_ACCOUNT)
# Iterate over each group (account)
for account, group in grouped_data:
# Initialize an empty list to store negative keywords for the current account
negative_keywords_for_account = []
# Iterate over each row in the group (account)
for index, row in group.iterrows():
# Get the current keyword
current_keyword = row[RPT_COL_KEYWORD]
# Get all other keywords in the account except the current one
other_keywords = group[group[RPT_COL_KEYWORD] != current_keyword][RPT_COL_KEYWORD].unique()
# Append other keywords as negative keywords for the current keyword
for other_keyword in other_keywords:
negative_keywords_for_account.append({
'Negative Keyword': other_keyword,
'Account': account,
'Campaign': row[RPT_COL_CAMPAIGN],
'Group': row[RPT_COL_GROUP],
'Match Type': 'Exact'
})
# Extend the list of negative keywords for the account
negative_keywords_by_account.extend(negative_keywords_for_account)
return negative_keywords_by_account
# Generate negative keywords for all accounts
all_negative_keywords = generate_negative_keywords(inputDf)
# Print the negative keywords
for keyword_info in all_negative_keywords:
print(f"Negative Keyword: {keyword_info['Negative Keyword']}, Account: {keyword_info['Account']}, Campaign: {keyword_info['Campaign']}, Group: {keyword_info['Group']}, Match Type: {keyword_info['Match Type']}")
Post generated on 2024-11-27 06:58:46 GMT