Script 687: Campaign Name Dimension Auto Tagging School
Purpose
The script parses campaign names to automatically tag them with a school identifier based on a specific naming convention.
To Elaborate
The Python script is designed to automate the process of tagging campaign names with a school identifier. It achieves this by parsing the campaign name and extracting a tag that appears before the first underscore (‘_’). This tag is then used to populate a specific column in a data frame, which represents the school associated with each campaign. The script ensures that only campaigns with a different tag than the existing one are updated, and it filters out any entries where the tag remains unchanged or is empty. This process helps maintain consistency and accuracy in campaign data management, particularly in environments where campaign names follow a structured format that includes school identifiers.
Walking Through the Code
- Configurable Parameters:
- The script begins by defining configurable parameters such as the separator (
SEP
) used in campaign names and the location of the tag (TAG_LOCATION
). - It specifies the primary data source and the relevant columns for input and output data frames.
- The script begins by defining configurable parameters such as the separator (
- Function Definition:
- A function
get_tag_from_campaign_name
is defined to extract the tag from a campaign name. It checks for the presence of the separator and splits the name accordingly to retrieve the tag.
- A function
- Data Processing:
- The script copies all input data to an output data frame to preserve the original data.
- It iterates over each row in the input data frame, extracting the campaign name and existing tag.
- Tag Extraction and Assignment:
- For each campaign name containing the separator, the script extracts the tag using the defined function.
- It prints the campaign and tag information for verification.
- If the extracted tag is different from the existing tag, it updates the output data frame with the new tag; otherwise, it assigns a NaN value.
- Filtering and Output:
- The script filters the output data frame to include only rows with non-empty tags.
- It checks if the output data frame is empty and prints the results accordingly.
Vitals
- Script ID : 687
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, School, Campaign ID
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-02-08 21:55
- Last Updated by dwaidhas@marinsoftware.com on 2024-11-21 16:19
> 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
##
## name: Campaign Name Dimension Auto Tagging: School
## description:
## Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Program.
## Tag appears before the first '_' underscore in campaign name.
##
## author: Dana Waidhas
## created: 2024-02-08
##
########### Configurable Params - START ##########
SEP = '_'
TAG_LOCATION = 1 # Comes after the first separator
# Primary data source and columns
inputDf = dataSourceDict["1"]
# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_SCHOOL = 'School'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
BULK_COL_SCHOOL = 'School'
def get_tag_from_campaign_name(campaign_name):
# Check if the separator is present in the campaign name
if SEP in campaign_name:
# Split only after the first underscore
vals = campaign_name.split(SEP, 1)
tag = vals[0].strip() if len(vals) > 0 else campaign_name.strip()
return tag
else:
# Return the entire campaign name if no separator is found
return campaign_name.strip()
# Copy all input rows to output
outputDf = inputDf.copy()
# Loop through all rows
for index, row in inputDf.iterrows():
existing_tag = row[RPT_COL_SCHOOL]
campaign_name = row[RPT_COL_CAMPAIGN]
# Skip processing if campaign name does not contain the separator
if SEP not in campaign_name:
continue
tag = get_tag_from_campaign_name(campaign_name)
# Print campaign and tag information
print("Campaign [%s] => Tag [%s]" % (campaign_name, tag))
# Only tag if it's different than the existing tag
if (len(tag) > 0) & (tag != existing_tag):
outputDf.at[index, BULK_COL_SCHOOL] = tag
else:
outputDf.at[index, BULK_COL_SCHOOL] = np.nan
# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_SCHOOL])
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-11-27 06:58:46 GMT