Script 695: Script Auto Tag Campaigns with Brand Non Brand Dimension
Purpose
The script automatically tags marketing campaigns as “Brand” or “Non-Brand” based on the campaign name, specifically tagging as “Non-Brand” if the name starts with ‘EEE’.
To Elaborate
The Python script is designed to categorize marketing campaigns into “Brand” or “Non-Brand” categories by analyzing the campaign names. The primary business rule is that any campaign name starting with ‘EEE’ is tagged as “Non-Brand”, while all other campaigns are tagged as “Brand”. This categorization helps in organizing and analyzing marketing efforts based on brand association. The script processes each campaign, checks its current tag, and updates it if necessary, ensuring that only campaigns with changed tags are included in the final output. This automated tagging process aids in maintaining consistent and accurate campaign data, which is crucial for strategic marketing decisions.
Walking Through the Code
- Function Definition:
- The script defines a function
get_brand_tag_from_campaign_name
that takes a campaign name as input and returns “Non-Brand” if the name starts with ‘EEE’, otherwise it returns “Brand”.
- The script defines a function
- Data Preparation:
- The script begins by copying the input DataFrame
inputDf
tooutputDf
to preserve the original data while making modifications.
- The script begins by copying the input DataFrame
- Iterating Through Campaigns:
- It loops through each row of the input DataFrame, extracting the campaign name and its existing brand tag.
- For each campaign, it determines the appropriate brand tag using the defined function.
- Tagging Logic:
- The script prints the campaign name and its determined tag for verification.
- It updates the brand tag in the output DataFrame only if the new tag differs from the existing one, ensuring that only necessary changes are made.
- Finalizing Output:
- The script removes any rows from the output DataFrame where the brand tag is not updated, ensuring that only relevant changes are included.
- It checks if the output DataFrame is empty and prints the results accordingly.
Vitals
- Script ID : 695
- Client ID / Customer ID: 1306927027 / 60270153
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Brand vs NonBrand
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-02-13 14:45
- Last Updated by dwaidhas@marinsoftware.com on 2024-02-28 20:15
> 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
##
## name: Script: Auto Tag Campaigns with Brand / Non-Brand Dimension
## description: Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Brand or Non-Brnad
## Tag for Non-Brand appears if Campaign Name starts with 'EEE'
##
##
## author: Dana Waidhas
## created: 2024-02-14
##
# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
RPT_CPL_STATUS = 'Campaign Status'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
def get_brand_tag_from_campaign_name(campaign_name):
# Check if the campaign name starts with 'EEE' and set as Non-Brand
if campaign_name.startswith("EEE"):
return "Non-Brand"
else:
return "Brand"
# Copy all input rows to output
outputDf = inputDf.copy()
# Loop through all rows
for index, row in inputDf.iterrows():
existing_tag = row[RPT_COL_BRAND_VS_NONBRAND]
campaign_name = row[RPT_COL_CAMPAIGN]
brand_tag = get_brand_tag_from_campaign_name(campaign_name)
# Print campaign and tag information
print("Campaign [%s] => Brand/Non-Brand Tag [%s]" % (campaign_name, brand_tag))
# Only tag if it's different than the existing tag
if (len(brand_tag) > 0) and (brand_tag != existing_tag):
outputDf.at[index, BULK_COL_BRAND_VS_NONBRAND] = brand_tag
else:
outputDf.at[index, BULK_COL_BRAND_VS_NONBRAND] = np.nan
# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_BRAND_VS_NONBRAND])
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-11-27 06:58:46 GMT