Script 1093: Script Campaign Country Code

Purpose

The script automates the extraction and tagging of the “Country Code” dimension from campaign names for Carburant’s Square Enix, excluding Meta publishers.

To Elaborate

The Python script is designed to automate the process of extracting and tagging the “Country Code” dimension from campaign names at the campaign level for Carburant’s Square Enix. It specifically targets campaigns from all publishers except Meta, which is handled by a different script. The script parses the campaign name to identify and extract the “Country Code” tag, which is expected to appear after the fifth hyphen (‘-‘) in the campaign name. This extracted tag is then used to update the corresponding “Country Code” field in the dataset, ensuring that only new or different tags are applied. The script also cleans up any extra whitespace in the campaign names and filters out any entries where the “Country Code” tag remains unchanged or is empty.

Walking Through the Code

  1. Configurable Parameters
    • The script begins by defining configurable parameters, including the PLACEMENT_KEY, which is set to a hyphen (‘-‘). This key is used to parse the campaign name.
    • It specifies the primary data source and the columns involved in the process, such as ‘Campaign’, ‘Account’, and ‘Country Code’.
  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 identifies the segment after the fifth hyphen.
    • If a match is found, the tag is returned; otherwise, the entire campaign name is returned if no hyphen is present.
  3. Data Processing
    • The script copies all input data to an output DataFrame for processing.
    • It iterates over each row in the input data, checking if the campaign name contains the PLACEMENT_KEY. If not, it skips processing for that row.
  4. Tagging Logic
    • For each campaign name containing the PLACEMENT_KEY, the script extracts the tag and compares it with the existing tag.
    • If the extracted tag is different and non-empty, it updates the “Country Code” field in the output DataFrame. Otherwise, it sets the field to NaN.
  5. Final Adjustments and Output
    • The script removes rows with empty “Country Code” tags from the output DataFrame.
    • It trims any extra whitespace from the campaign names to ensure data consistency.
    • Finally, it checks if the output DataFrame is empty and prints the results accordingly.

Vitals

  • Script ID : 1093
  • Client ID / Customer ID: 1306927809 / 60270355
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Country Code
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-05-14 13:58
  • Last Updated by Grégory Pantaine on 2024-05-22 15:09
> 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 the fifth '-' in campaign name.
## 
## Copied by Grégory Pantaine
## created: 2024-05-15

########### 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_COUNTRY_CODE = 'Country Code'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_COUNTRY_CODE = 'Country Code'

# Function to extract tag from campaign name

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

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

# 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