Script 1429: Products Dimension Tagging Ads
Purpose
Automatically tags the “Product” dimension based on the product detail page URL of an ad.
To Elaborate
The Python script is designed to automate the process of tagging a “Product” dimension in a dataset based on the URL of a product detail page from an advertisement. This is particularly useful for businesses that need to categorize or label their products in advertising data for better analysis and reporting. The script reads a dataset containing various columns, including a “Destination URL,” and applies a mapping function to determine the product type based on specific keywords found in the URL. The result is a new dataset where each entry is tagged with a corresponding product name, facilitating more organized and insightful data analysis.
Walking Through the Code
-
Data Initialization: The script begins by defining the primary data source and relevant columns from the input dataset. It sets up the input DataFrame (
inputDf
) and specifies the columns that will be used for processing, such as ‘Destination URL’ and ‘Product’. -
Output DataFrame Setup: An output DataFrame (
outputDf
) is initialized as a copy of the input DataFrame. This ensures that the original data remains unchanged while the script processes and updates the product tags. -
Mapping Function Definition: A function named
map_product
is defined to map URLs to specific product names. It checks for specific keywords in the ‘Destination URL’ and returns a corresponding product name. If no keywords match, it defaults to ‘Unknown Product’. -
Applying the Mapping Function: The script applies the
map_product
function to the ‘Destination URL’ column of the input DataFrame. The results are stored in the ‘Product’ column of the output DataFrame, effectively tagging each entry with the appropriate product name. -
Result Display: Finally, the script prints the output DataFrame, showcasing the newly tagged product information for each entry.
Vitals
- Script ID : 1429
- Client ID / Customer ID: 1306928175 / 60270415
- Action Type: Bulk Upload
- Item Changed: Ad
- Output Columns: Account, Campaign, Group, Creative ID, Product
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jeff Sands (jsands@marinsoftware.com)
- Created by Jeff Sands on 2024-10-09 20:53
- Last Updated by Jeff Sands on 2024-10-09 20:56
> 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
##
## name: Products Dimension Tagging - Ads
## description:
## Automatically tag the dimension "Product" with a specific value based on the
## product detail page URL of an ad.
## author: Jeff Sands
## created: 2024-10-09
##
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_GROUP = 'Group'
RPT_COL_STATUS = 'Status'
RPT_COL_PUB_ID = 'Pub. ID'
RPT_COL_CREATIVE_ID = 'Creative ID'
RPT_COL_DESTINATION_URL = 'Destination URL'
RPT_COL_PRODUCT = 'Product'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_CREATIVE_ID = 'Creative ID'
BULK_COL_PRODUCT = 'Product'
outputDf[BULK_COL_PRODUCT] = "<<YOUR VALUE>>"
# user code start here
print(tableize(inputDf.head()))
# Output DataFrame initialization
outputDf = inputDf.copy()
# Mapping URLs to Product values
def map_product(url):
if 'age-defying-serum' in url:
return 'Age Defying Serum'
elif 'eye-refresh' in url:
return 'Eye Refresh'
elif 'gentle-serum' in url:
return 'Gentle Serum'
elif 'sculpting-cream' in url:
return 'Sculpting Cream'
elif 'the-essential-gentle-bundle' in url:
return 'Essential Gentle Bundle'
elif 'the-essential-original-bundle' in url:
return 'Essential Original Bundle'
else:
return 'Unknown Product'
# Apply the mapping function to the 'Destination URL' column and populate 'Product' column
outputDf[RPT_COL_PRODUCT] = inputDf[RPT_COL_DESTINATION_URL].apply(map_product)
# Display the result
print(outputDf)
Post generated on 2024-11-27 06:58:46 GMT