Script 969: Go Puff Script Campaign Category Dimension Tag
Purpose
Python script to parse campaign names and add campaign-level Marin Dimensions Tag for Category.
To Elaborate
The Python script solves the problem of extracting a specific tag from campaign names and adding it as a dimension tag for the category. The script searches for the tag after the “cat:” keyword in the campaign name and adds it to the output dataframe. The script also handles cases where the campaign name does not contain the placement key or if the tag is already present in the existing tag column. The output dataframe is then filtered to include only non-empty tags and any extra whitespace in the campaign name is removed.
Walking Through the Code
- The script starts by defining the configurable parameters, such as the placement key and the input dataframe.
- A function named
get_tag_from_campaign_name
is defined to extract the tag from the campaign name using a regular expression pattern. - The input dataframe is copied to the output dataframe.
- The script iterates through each row of the input dataframe.
- For each row, it checks if the campaign name contains the placement key. If not, it skips processing for that row.
- If the campaign name contains the placement key, the script calls the
get_tag_from_campaign_name
function to extract the tag. - If the extracted tag is different from the existing tag in the output dataframe, it updates the tag column for that row.
- After iterating through all rows, the script drops any rows with empty tags from the output dataframe.
- Any extra whitespace in the campaign name column is removed.
- Finally, the script prints the tableized output dataframe if it is not empty, otherwise it prints “Empty outputDf”.
Vitals
- Script ID : 969
- Client ID / Customer ID: 1306927677 / 60270331
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Autotag-Category
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
- Created by Grégory Pantaine on 2024-04-12 15:46
- Last Updated by Grégory Pantaine on 2024-04-12 16:09
> 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
## name: Campaign Name Dimension Auto Tagging: Autotag-Category
## description:
## Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Category.
## Tag appears after 'cat:' in campaign name.
##
## Copied by Grég P, created by the one and only Michael H!
## created: 2024-03-27
########### Configurable Params - START ##########
PLACEMENT_KEY = 'cat:'
# Primary data source and columns
inputDf = dataSourceDict["1"]
# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_PLACEMENT = 'Autotag-Category'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PLACEMENT = 'Autotag-Category'
# Function to extract tag from campaign name
def get_tag_from_campaign_name(campaign_name):
regex_pattern = r"cat:(\w+)"
placement_match = re.search(regex_pattern, campaign_name)
if placement_match:
placement_value = placement_match.group(1)
# print("Placement value:", placement_value)
return placement_value.strip()
else:
print("Placement value not found: " + campaign_name)
# Return the entire campaign name if 'pl:' is not present
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_PLACEMENT]
campaign_name = row[RPT_COL_CAMPAIGN]
# Skip processing if campaign name does not contain the placement key
if PLACEMENT_KEY 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_PLACEMENT] = tag
else:
outputDf.at[index, BULK_COL_PLACEMENT] = np.nan
# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_PLACEMENT])
# Remove extra whitespace from campaign name that breaks Preview
outputDf[RPT_COL_CAMPAIGN] = outputDf[RPT_COL_CAMPAIGN].str.strip()
if not outputDf.empty:
print("outputDf", tableize(outputDf.head()))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT