Script 1071: Script Campaign Type2Campagne
Purpose
Python script that parses campaign names and extracts the campaign type into a dimension called “Type de campagne”.
To Elaborate
The Python script solves the problem of extracting the campaign type from campaign names. The campaign type is identified by a tag that appears after a hyphen in the campaign name. The script takes an input DataFrame that contains campaign data and outputs a modified DataFrame with the campaign type extracted into a new column called “Type de campagne”. The script follows the following business rules:
- The campaign type tag appears after a hyphen in the campaign name.
- If the campaign name does not contain a hyphen, it is skipped.
- If the extracted campaign type is different from the existing campaign type in the DataFrame, it is updated.
- The output DataFrame only includes rows with non-empty campaign types.
- Extra whitespace in the campaign name is removed to avoid issues with previewing the data.
The script uses the following user-changeable parameters:
PLACEMENT_KEY
: The character that indicates the placement of the campaign type tag in the campaign name.inputDf
: The input DataFrame that contains the campaign data.RPT_COL_CAMPAIGN
: The column name in the input DataFrame that contains the campaign names.RPT_COL_TYPE_CAMPAGNE
: The column name in the input DataFrame where the campaign type will be extracted.BULK_COL_TYPE_CAMPAGNE
: The column name in the output DataFrame where the campaign type will be stored.
Walking Through the Code
- The script defines a function
get_tag_from_campaign_name
that extracts the campaign type tag from a campaign name using a regular expression pattern. - The script creates an output DataFrame by copying the input DataFrame.
- The script loops through each row in the input DataFrame.
- For each row, it retrieves the existing campaign type and campaign name.
- If the campaign name does not contain the placement key, the row is skipped.
- The script calls the
get_tag_from_campaign_name
function to extract the campaign type tag from the campaign name. - If the extracted tag is different from the existing campaign type, it is updated in the output DataFrame.
- The script drops rows with empty campaign types from the output DataFrame.
- Extra whitespace in the campaign name is removed in the output DataFrame.
- If the output DataFrame is not empty, it is printed. Otherwise, a message indicating an empty output is printed.
Vitals
- Script ID : 1071
- Client ID / Customer ID: 1306927809 / 60270355
- Action Type: Bulk Upload (Preview)
- 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-10 17:02
> 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-05-15 07:44:05 GMT