Script 855: Go Puff Script Campaign Placement Dimension Tag
Purpose
Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Placement.
To Elaborate
The Python script solves the problem of parsing campaign names and adding campaign-level Marin Dimensions tags for placement. It extracts the tag from 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.
Walking Through the Code
- The script starts by defining the configurable parameters, such as the placement key and the input and output columns.
- It defines a function called
get_tag_from_campaign_name
that uses regular expressions to extract the tag from the campaign name. - The script then creates an output dataframe by copying the input dataframe.
- It loops through each row in 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, it calls the
get_tag_from_campaign_name
function to extract the tag. - It compares the extracted tag with the existing tag in the row.
- If the extracted tag is different from the existing tag, it updates the output dataframe with the new tag.
- The script then drops rows with empty tags from the output dataframe.
- It removes extra whitespace from the campaign names in the output dataframe.
- Finally, it prints the output dataframe if it is not empty, or prints “Empty outputDf” if it is empty.
Vitals
- Script ID : 855
- Client ID / Customer ID: 1306927677 / 60270331
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Autotag-Placement
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
- Created by Grégory Pantaine on 2024-03-27 11:11
- Last Updated by Grégory Pantaine on 2024-04-12 16:10
> 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-Placement
## description:
## Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Placement.
## Tag appears after 'pl:' in campaign name.
##
## Copied by Grégory Pantaine
## created: 2024-03-27
########### Configurable Params - START ##########
PLACEMENT_KEY = 'pl:'
# 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-Placement'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PLACEMENT = 'Autotag-Placement'
# Function to extract tag from campaign name
def get_tag_from_campaign_name(campaign_name):
regex_pattern = r"pl:(\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