Script 967: Go Puff Script Campaign Type Dimension Tag

Purpose

Python script to parse campaign names and add campaign-level Marin Dimensions Tag for campaign type.

To Elaborate

The Python script solves the problem of extracting campaign-level Marin Dimensions Tag for campaign type from campaign names. It parses the campaign names and adds the tag after ‘ct:’ in the campaign name. The script skips processing if the campaign name does not contain the placement key. It also checks if the tag is different from the existing tag before tagging. The script removes extra whitespace from the campaign name to avoid breaking the Preview. The output is a DataFrame with the tagged campaign names.

Walking Through the Code

  1. The script defines the configurable parameter PLACEMENT_KEY which is set to ‘ct:’.
  2. It defines the input DataFrame inputDf as the primary data source.
  3. It defines the output columns and initial values.
  4. It defines a function get_tag_from_campaign_name to extract the tag from the campaign name using a regex pattern.
  5. The script creates a copy of the input DataFrame as the output DataFrame outputDf.
  6. It loops through all rows in the input DataFrame.
  7. For each row, it retrieves the existing tag and campaign name.
  8. If the campaign name does not contain the placement key, it skips processing.
  9. It calls the get_tag_from_campaign_name function to extract the tag from the campaign name.
  10. If the tag is different from the existing tag, it updates the BULK_COL_PLACEMENT column in the output DataFrame with the tag.
  11. It drops rows with empty tags from the output DataFrame.
  12. It removes extra whitespace from the campaign name in the output DataFrame.
  13. If the output DataFrame is not empty, it prints the tableized head of the DataFrame. Otherwise, it prints “Empty outputDf”.

Vitals

  • Script ID : 967
  • Client ID / Customer ID: 1306927677 / 60270331
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Autotag-CampaignType
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-04-12 15:45
  • 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-CampaignType
## description:
## Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Campaign type.
## Tag appears after 'ct:' 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 = 'ct:'

# 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-CampaignType'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_PLACEMENT = 'Autotag-CampaignType'

# Function to extract tag from campaign name

def get_tag_from_campaign_name(campaign_name):
    regex_pattern = r"ct:(\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

comments powered by Disqus