Script 1341: Dimension Update Campaign & Medium
Purpose
Automates the process of populating utmcampaign
and utmmedium
dimensions based on campaign name and campaign type values, respectively.
To Elaborate
The Python script is designed to automate the process of populating two specific dimensions, utmcampaign
and utmmedium
, based on the values of campaign name and campaign type. This is particularly useful in digital marketing and analytics, where tracking the source and type of campaigns is crucial for performance analysis. The script processes a dataset, filtering out rows where these dimensions are already populated, and applies specific rules to fill in the missing values. The utmcampaign
is derived by transforming the campaign name to a standardized format, while the utmmedium
is determined based on predefined rules associated with the campaign type. This ensures consistent and accurate data for reporting and analysis.
Walking Through the Code
- Data Preparation
- The script begins by defining the primary data source and relevant columns from the dataset. It identifies columns for campaign, account, campaign type, and the dimensions to be populated (
utmcampaign
andutmmedium
).
- The script begins by defining the primary data source and relevant columns from the dataset. It identifies columns for campaign, account, campaign type, and the dimensions to be populated (
- Function Definitions
- Two functions are defined:
set_utm_campaign
andset_utm_medium
.set_utm_campaign
: Converts the campaign name to lowercase and replaces spaces with plus signs.set_utm_medium
: Determines theutmmedium
value based on the campaign type, using specific rules (e.g., “Search” becomes “paidsearch”).
- Two functions are defined:
- Data Filtering
- The script filters the input data to exclude rows where both
utmcampaign
andutmmedium
are already populated. This filtered data is stored in a new dataframe,filteredDf
.
- The script filters the input data to exclude rows where both
- Data Transformation
- The script applies the defined functions to the filtered data, updating the
utmcampaign
andutmmedium
columns in the output dataframe,outputDf
.
- The script applies the defined functions to the filtered data, updating the
- Output
- Finally, the script prints a preview of the transformed data, showcasing the first few rows of the updated dataframe.
Vitals
- Script ID : 1341
- Client ID / Customer ID: 1306913420 / 60268008
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, utmcampaign, utmmedium
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Kyle Perkins (kyle.perkins@genesys.com)
- Created by Kyle Perkins on 2024-08-20 14:15
- Last Updated by Kyle Perkins on 2024-08-20 14:22
> 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
##
## name: Campaign Dimension Update
## description:
## Automates populating utmcampaign and utmmedium dimensions based on campaign name and campaign type values, respectively
##
## author: Byron Porter
## created: 2024-07-24
##
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_TYPE = 'Campaign Type'
RPT_COL_UTMMEDIUM = 'utmmedium'
RPT_COL_UTMCAMPAIGN = 'utmcampaign'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_UTMCAMPAIGN = 'utmcampaign'
BULK_COL_UTMMEDIUM = 'utmmedium'
# define function to replace ' ' with '+' and convert to lowercase in Campaign value
def set_utm_campaign(campaign_name):
return campaign_name.replace(' ', '+').lower()
# define function to determine utmmedium dimension value based on Campaign Type value
def set_utm_medium(campaign_type):
if campaign_type == "Search":
return "paidsearch"
elif campaign_type in ["Display", "Video", "Discovery"]:
return "display"
elif campaign_type == "Performance Max":
return "paidsearch"
else:
return None
# filter inputDf to skip rows where both utmmedium and utmcampaign are populated and copy rows to new filteredDf dataframe
filteredDf = inputDf[
~inputDf[RPT_COL_UTMMEDIUM].notna() | ~inputDf[RPT_COL_UTMCAMPAIGN].notna()
].copy()
# intialize outputDf so that it only includes rows that have been filtered
outputDf = filteredDf
# apply the functions to each row in the filtered dataframe and set result as the output dataframe value
outputDf[BULK_COL_UTMCAMPAIGN] = filteredDf[RPT_COL_CAMPAIGN].apply(set_utm_campaign)
outputDf[BULK_COL_UTMMEDIUM] = filteredDf[RPT_COL_CAMPAIGN_TYPE].apply(set_utm_medium)
print(tableize(outputDf.head()))
Post generated on 2024-11-27 06:58:46 GMT