Script 675: Campaign Name Dimension Auto Tagging Region

Purpose

Python script to parse campaign names and add a campaign-level Marin Dimensions tag for the region.

To Elaborate

The Python script solves the problem of parsing campaign names and adding a Marin Dimensions tag for the region. The script takes a primary data source, which is a DataFrame, and extracts the region tag from the campaign name. The tag is added to the output DataFrame, which includes columns for campaign, account, and region. The script also includes additional logic to extract a specific format of the region tag from the campaign name. The output DataFrame is then printed, showing the campaign name and corresponding tag.

Walking Through the Code

  1. The script starts by defining configurable parameters, such as the separator used in the campaign name and the location of the tag within the campaign name.
  2. The script defines a function called get_tag_from_campaign_name that takes a campaign name as input and returns the extracted tag.
  3. The script creates an output DataFrame by copying the input DataFrame.
  4. The script iterates through each row of the input DataFrame.
  5. For each row, the script extracts the existing tag, campaign name, and tag from the campaign name using the get_tag_from_campaign_name function.
  6. The script checks if the extracted tag is different from the existing tag.
  7. If the extracted tag is different and not empty, it updates the corresponding row in the output DataFrame with the extracted tag.
  8. If the extracted tag is empty or the same as the existing tag, it updates the corresponding row in the output DataFrame with NaN.
  9. The script drops rows with NaN values in the region column from the output DataFrame.
  10. If the output DataFrame is not empty, it prints the tableized version of the DataFrame.
  11. If the output DataFrame is empty, it prints “Empty outputDf”.

Vitals

  • Script ID : 675
  • Client ID / Customer ID: 1306913045 / 60268001
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Region
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
  • Created by dwaidhas@marinsoftware.com on 2024-02-01 15:42
  • Last Updated by dwaidhas@marinsoftware.com on 2024-02-01 18: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
72
73
74
##
## Name: Campaign Name - Dimension Auto Tagging: Region
## Description:
##  Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Region
##  Tag appears after the first ',' comma in campaign name.
##
## author: Dana Waidhas
## created: 2024-01-31
##


########### 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_REGION = 'Region'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_REGION = 'Region'


def get_tag_from_campaign_name(campaign_name):
    # Split only at the first comma
    vals = campaign_name.split(SEP, 1)
    tag = vals[1].strip() if len(vals) > 1 else ''


    # Additional logic to extract 'USA - [Local] - Nashville' from the campaign name
    if ' - USA - [Local] - ' in campaign_name:
        tag += 'USA - [Local] - ' + re.search(r'- USA - \[Local\] - (.*?) - ', campaign_name).group(1)


    # Remove '2017' if present in the tag using regular expression
    tag = re.sub(r'\b2017\b', '', tag)


    return tag.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_REGION]
    campaign_name = row[RPT_COL_CAMPAIGN]
    tag = get_tag_from_campaign_name(campaign_name)
    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_REGION] = tag
    else:
        outputDf.at[index, BULK_COL_REGION] = np.nan


# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_REGION])


if not outputDf.empty:
    print("outputDf", tableize(outputDf))
else:
    print("Empty outputDf")


Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus