Script 605: Campaign Geo Assignment
Purpose
The Python script assigns geographic values to a campaign’s Geo dimension based on specific keywords in the campaign name.
To Elaborate
The script is designed to automate the process of assigning geographic locations to marketing campaigns based on the campaign names. This is particularly useful for businesses that run multiple campaigns across different regions and need to categorize them accordingly for reporting or analysis purposes. The script examines the campaign names for specific keywords or patterns that correspond to various geographic locations, such as states or regions. When a match is found, the script assigns the appropriate geographic value to the campaign’s Geo dimension. This ensures that each campaign is accurately tagged with its corresponding location, facilitating better organization and analysis of campaign performance across different regions.
Walking Through the Code
- Data Preparation:
- The script begins by defining the primary data source and relevant columns, which include campaign details and various performance metrics.
- An output DataFrame is initialized with a placeholder value for the Geo column.
- Geo Assignment Function:
- A function named
update_geo
is defined to determine the geographic location based on the campaign name. - The function converts the campaign name to lowercase and checks for specific keywords or patterns that indicate a particular geographic location.
- If a match is found, the function returns the corresponding geographic location; otherwise, it returns an empty string.
- A function named
- Application of the Function:
- The script retrieves the input data from a predefined data source.
- It applies the
update_geo
function to each row of the output DataFrame, updating the Geo column with the appropriate geographic value based on the campaign name.
- User Changeable Parameters:
- Users can modify the keywords and patterns within the
update_geo
function to accommodate additional geographic locations or different naming conventions used in campaign names.
- Users can modify the keywords and patterns within the
Vitals
- Script ID : 605
- Client ID / Customer ID: 1306926715 / 60270103
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Geo
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Chris Jetton (cjetton@marinsoftware.com)
- Created by Chris Jetton on 2023-12-18 21:47
- Last Updated by Chris Jetton on 2024-01-18 20:18
> 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: Campaign Geo Assignment
## description: assigns values to Geo dimension at campaign level when appropriate
##
##
## author: Chris Jetton
## created: 2023-12-18
##
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_GEO = 'Geo'
RPT_COL_META_ABO_BUDGET = 'Meta ABO Budget'
RPT_COL_BUDGET_LEVEL = 'Budget Level'
RPT_COL_PUBLISHER_NAME = 'Publisher Name'
RPT_COL_PUB_ID = 'Pub. ID'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_IMPR = 'Impr.'
RPT_COL_CLICKS = 'Clicks'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_CTR = 'CTR %'
RPT_COL_ENROLLMENTUPLOAD_CONV = 'enrollment-upload Conv.'
RPT_COL_LEADUPLOAD_CONV = 'Lead-upload Conv.'
RPT_COL_LEADTRACKER_CONV = 'Lead-tracker Conv.'
RPT_COL_OF_LEAD_CONV = '# of Lead Conv.'
RPT_COL_INSTANTFORM_LEADS_CONV = 'InstantForm Leads Conv.'
RPT_COL_WEBSITE_LEADS_CONV = 'Website Leads Conv.'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GEO = 'Geo'
outputDf[BULK_COL_GEO] = "<<YOUR VALUE>>"
# Function to update Geo column based on campaign name
def update_geo(row):
campaign = row['Campaign'].lower()
if 'indiana' in campaign or 'in_' in campaign or 'in22' in campaign:
return 'Indiana'
elif 'kansas' in campaign or 'ks20' in campaign:
return 'Kansas'
elif 'sc22' in campaign:
return 'South Carolina'
elif 'oh22' in campaign or 'ohio' in campaign or 'engage oh' in campaign or 'engage_oh' in campaign:
return 'Ohio'
elif 'colorado' in campaign:
return 'Colorado'
elif 'az21' in campaign or 'engage az' in campaign or 'ar21' in campaign:
return 'Arizona'
elif 'wi_' in campaign:
return 'Wisconsin'
elif 'nh_' in campaign:
return 'New Hampshire'
elif 'mi adult' in campaign or 'mi_' in campaign:
return 'Michigan'
elif 'mo21' in campaign:
return 'Missouri'
else:
return ''
# primary data source and columns
inputDf = dataSourceDict["1"]
# Apply the function to update Geo column
outputDf['Geo'] = outputDf.apply(update_geo, axis=1)
Post generated on 2024-11-27 06:58:46 GMT