Script 1071: Script Campaign Type2Campagne

Purpose

The script extracts and assigns a campaign type from a campaign name based on a specific tag format.

To Elaborate

The Python script is designed to parse campaign names and extract a specific tag that represents the campaign type. This tag is expected to appear after a hyphen (‘-‘) in the campaign name. The extracted tag is then used to populate a dimension called ‘Type de Campagne’. The script processes each campaign name, checks for the presence of the tag, and updates the campaign type if the extracted tag differs from the existing one. This ensures that the campaign type dimension accurately reflects the information encoded in the campaign name, facilitating better organization and analysis of campaign data.

Walking Through the Code

  1. Configurable Parameters:
    • The script begins by defining a configurable parameter PLACEMENT_KEY, which is set to a hyphen (‘-‘). This key is used to identify the position of the tag within the campaign name.
    • It also specifies the primary data source and the relevant columns for input and output, such as ‘Campaign’, ‘Account’, and ‘Type de Campagne’.
  2. Tag Extraction Function:
    • A function get_tag_from_campaign_name is defined to extract the tag from the campaign name using a regular expression. The regex pattern looks for the tag after the third hyphen in the campaign name.
    • If a tag is found, it is returned after stripping any extra whitespace. If not, the entire campaign name is returned.
  3. Data Processing:
    • The script creates a copy of the input data frame to work on.
    • It iterates over each row of the input data frame, checking if the campaign name contains the PLACEMENT_KEY.
    • If the key is present, the script extracts the tag using the defined function and compares it with the existing tag in the ‘Type de Campagne’ column.
    • If the extracted tag is different and non-empty, it updates the ‘Type de Campagne’ column with the new tag. Otherwise, it sets the value to NaN.
  4. Final Adjustments:
    • The script removes rows with empty tags from the output data frame to ensure only relevant data is retained.
    • It also strips any extra whitespace from the campaign names to prevent issues in data preview.
    • Finally, it checks if the output data frame is empty and prints the first few rows if it contains data.

Vitals

  • Script ID : 1071
  • Client ID / Customer ID: 1306927809 / 60270355
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Type de Campagne
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-05-10 16:50
  • Last Updated by Grégory Pantaine on 2024-05-15 12:27
> 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: Script - Campaign - Type de Campagne
## description:
## Parse Campaign Name and pick out the campaign type into a dimension 'Type de campagne'.
## Tag appears after '-' in campaign name.
## 
## Copied by Grégory Pantaine
## created: 2024-05-10

########### Configurable Params - START ##########
PLACEMENT_KEY = '-'

# Primary data source and columns
inputDf = dataSourceDict["1"]

# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TYPE_CAMPAGNE = 'Type de Campagne'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TYPE_CAMPAGNE = 'Type de Campagne'

# Function to extract tag from campaign name

def get_tag_from_campaign_name(campaign_name):
    regex_pattern = r"(?:[^-]+-){3}(\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_TYPE_CAMPAGNE]
    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_TYPE_CAMPAGNE] = tag
    else:
        outputDf.at[index, BULK_COL_TYPE_CAMPAGNE] = np.nan

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

# 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-11-27 06:58:46 GMT

comments powered by Disqus