Script 689: Campaign Name Dimension Auto Tagging Program
Purpose
The script parses campaign names to automatically tag them with a program identifier based on a specific naming convention.
To Elaborate
The Python script is designed to automate the process of tagging campaign names with a program identifier. It does this by parsing the campaign name and extracting a tag that appears after the first underscore (‘_’) in the name. This tag is then used to populate a specific column in the data, ensuring that each campaign is correctly associated with its respective program. The script checks each campaign name for the presence of the separator and only processes those that contain it. If the extracted tag differs from the existing tag in the data, it updates the tag; otherwise, it leaves the entry blank. This ensures that only relevant and updated tags are included in the final output.
Walking Through the Code
- Configurable Parameters:
- The script begins by defining configurable parameters such as the separator (
SEP
) and the tag location (TAG_LOCATION
). These parameters determine how the campaign name is parsed. - The primary data source is defined, and relevant columns for input and output are specified.
- The script begins by defining configurable parameters such as the separator (
- Tag Extraction Function:
- A function
get_tag_from_campaign_name
is defined to extract the tag from the campaign name. It checks for the presence of the separator and splits the name accordingly. - The function extracts the first five characters following the separator as the tag.
- A function
- Data Processing:
- The script copies the input data to an output DataFrame to preserve the original data.
- It iterates over each row in the input data, extracting the campaign name and existing tag.
- Tagging Logic:
- 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 DataFrame with the new tag. Otherwise, it sets the entry to NaN.
- Final Output:
- The script filters the output DataFrame to include only rows with non-empty tags.
- It prints the final output, ensuring that only campaigns with updated tags are included.
Vitals
- Script ID : 689
- Client ID / Customer ID: 1306926629 / 60270083
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Program, Campaign ID
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-02-09 19:20
- Last Updated by dwaidhas@marinsoftware.com on 2024-11-21 16:17
> 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
##
## name: Campaign Name Dimension Auto Tagging: Program
## description:
## Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Program.
## Tag appears after the first '_' underscore in campaign name.
##
## author: Dana Waidhas
## created: 2024-02-09
##
########### 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_PROGRAM = 'Program'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
BULK_COL_PROGRAM = 'Program'
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)
# Extract the first 5 letters after the first underscore
tag = vals[1][:5].strip() if len(vals) > 1 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_PROGRAM]
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_PROGRAM] = tag
else:
outputDf.at[index, BULK_COL_PROGRAM] = np.nan
# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_PROGRAM])
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-11-27 06:58:46 GMT