Script 695: Script Auto Tag Campaigns with Brand Non Brand Dimension

Purpose:

The script automatically tags 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 automate the process of tagging marketing campaigns with a “Brand” or “Non-Brand” label based on the campaign name. The primary business rule implemented in this script is that any campaign name starting with ‘EEE’ is tagged as “Non-Brand”. This helps in categorizing campaigns for better analysis and reporting. The script processes each campaign, checks its name, and assigns the appropriate tag. If the newly determined tag differs from the existing one, it updates the tag; otherwise, it leaves it unchanged. This ensures that only necessary updates are made, optimizing the tagging process.

Walking Through the Code

  1. Function Definition:
    • The script defines a function get_brand_tag_from_campaign_name that checks if a campaign name starts with ‘EEE’. If it does, it returns “Non-Brand”; otherwise, it returns “Brand”.
  2. Data Preparation:
    • The script begins by copying all input data rows to an output DataFrame, ensuring that the original data remains unchanged.
  3. Iterating Through Campaigns:
    • It loops through each row of the input DataFrame, extracting the campaign name and existing brand tag.
    • For each campaign, it determines the appropriate brand tag using the defined function.
  4. Tagging Logic:
    • If the determined brand tag is different from the existing tag, it updates the tag in the output DataFrame.
    • If the tag remains unchanged, it assigns a NaN value to avoid unnecessary updates.
  5. Final Output:
    • The script filters out any rows with empty tags in the output DataFrame to ensure only relevant data is retained.
    • It then 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 2025-03-11 01:25:51 GMT

comments powered by Disqus