Script 1291: Campaign Dimension Update

Purpose

Automates the process of populating utmcampaign and utmmedium dimensions based on campaign name and campaign type values.

To Elaborate

The Python script is designed to automate the task of populating two specific dimensions, utmcampaign and utmmedium, based on the values of campaign name and campaign type, respectively. This is particularly useful in digital marketing and analytics, where tracking and categorizing campaigns accurately is crucial for performance analysis. The script processes a dataset, filtering out rows where these dimensions are already populated, and applies specific transformation rules to fill in the missing values. The utmcampaign dimension is derived by replacing spaces with plus signs and converting the campaign name to lowercase. The utmmedium dimension is determined based on the campaign type, with predefined mappings for different types such as “Search”, “Display”, and “Performance Max”. This automation ensures consistency and accuracy in data reporting, saving time and reducing manual errors.

Walking Through the Code

  1. 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 and utmmedium).
  2. Function Definitions
    • Two functions are defined: set_utm_campaign and set_utm_medium.
      • set_utm_campaign transforms the campaign name by replacing spaces with plus signs and converting it to lowercase.
      • set_utm_medium assigns a value to utmmedium based on the campaign type, using predefined rules for different campaign types.
  3. Data Filtering
    • The script filters the input data to exclude rows where both utmcampaign and utmmedium are already populated. This filtered data is copied to a new dataframe, filteredDf.
  4. Data Transformation
    • The script initializes outputDf with the filtered data and applies the transformation functions to populate the utmcampaign and utmmedium columns. The results are stored in outputDf.
  5. Output
    • Finally, the script prints a tabular representation of the first few rows of the transformed data, showcasing the updated dimensions.

Vitals

  • Script ID : 1291
  • Client ID / Customer ID: 1306913420 / 60268008
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, utmmedium, utmcampaign
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Byron Porter (bporter@marinsoftware.com)
  • Created by Byron Porter on 2024-07-24 22:21
  • 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

comments powered by Disqus