Script 1021: Tagging dimension Script Campaigns Keyword Match Type

Purpose:

The script extracts keyword match types from campaign names and updates a data frame accordingly.

To Elaborate

The Python script is designed to process a data frame containing campaign information and extract keyword match types from the campaign names. The keyword match type is identified by specific intent keywords (such as ‘tofu’, ‘mofu’, ‘bofu’) followed by a separator. The script updates the data frame by adding the extracted keyword match type to a designated column. It ensures that only rows with valid keyword match types are retained, effectively filtering out incomplete data. This process aids in organizing and structuring campaign data for further analysis or reporting.

Walking Through the Code

  1. Define Configurable Parameters:
    • The script begins by defining several constants, including INTENT_KEYWORDS, which are the keywords used to identify the intent within campaign names, and SEP, the separator used in the campaign names.
  2. Function Definition:
    • A function extract_keyword_match_type is defined to extract the keyword match type from a given campaign name. It searches for the intent keywords followed by the separator and extracts the match type accordingly.
  3. Data Frame Copy:
    • The script creates a copy of the input data frame (inputDf) to outputDf, which will be modified and eventually outputted.
  4. Iterate Through Rows:
    • The script iterates over each row in the input data frame. For each campaign name, it calls the extract_keyword_match_type function to determine the keyword match type.
  5. Update Data Frame:
    • The extracted keyword match type is then used to update the corresponding column in the outputDf.
  6. Filter Rows:
    • The script removes any rows from outputDf that have missing keyword match type values, ensuring that only complete data is retained.
  7. Output Check:
    • Finally, the script checks if outputDf is empty and prints the data frame if it contains data, or a message indicating it is empty.

Vitals

  • Script ID : 1021
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Keyword Match Type
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-29 19:44
  • Last Updated by Autumn Archibald on 2024-04-29 19:51
> 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
# Define the configurable parameters for the script
INTENT_KEYWORDS = ['tofu', 'mofu', 'bofu']
SEP = '-'  # Separator used after intent keywords
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_KEYWORD_MATCH_TYPE = 'Keyword Match Type'

# Function to extract keyword match type from campaign name
def extract_keyword_match_type(campaign_name):
    for intent in INTENT_KEYWORDS:
        separator = intent + SEP
        if separator in campaign_name:
            start_index = campaign_name.find(separator) + len(separator)
            end_index = campaign_name.find(SEP, start_index)
            if end_index != -1:
                return campaign_name[start_index:end_index]
            else:
                return campaign_name[start_index:]
    return ''

# Copy input rows to output
outputDf = inputDf.copy()

# Loop through all rows
for index, row in inputDf.iterrows():
    campaign_name = row[RPT_COL_CAMPAIGN]

    # Extract keyword match type from campaign name
    keyword_match_type = extract_keyword_match_type(campaign_name)
    print("Campaign [%s] => Keyword Match Type [%s]" % (campaign_name, keyword_match_type))

    # Update keyword match type column
    outputDf.at[index, BULK_COL_KEYWORD_MATCH_TYPE] = keyword_match_type

# Drop any rows with missing keyword match type values
outputDf = outputDf.dropna(subset=[BULK_COL_KEYWORD_MATCH_TYPE])

if not outputDf.empty:
    print("outputDf", tableize(outputDf))
else:
    print("Empty outputDf")

Post generated on 2025-03-11 01:25:51 GMT

comments powered by Disqus