Script 1351: Dimension Update Device and Match Type

Purpose

Automates the process of populating device and match type dimensions based on campaign name values in a dataset.

To Elaborate

The Python script is designed to automate the task of determining and populating device and match type dimensions for marketing campaigns based on the campaign name and type. It processes a dataset containing campaign information and applies specific rules to infer the device type (e.g., mobile, desktop) and match type (e.g., exact, phrase) from the campaign name. The script ensures that these dimensions are only populated for campaigns where they are not already specified, thereby streamlining the data preparation process for marketing analysis and reporting. This automation helps in maintaining consistency and accuracy in the categorization of campaigns, which is crucial for effective budget allocation and performance tracking.

Walking Through the Code

  1. Data Preparation
    • The script begins by defining the primary data source and relevant columns from the dataset, such as campaign, account, and campaign type.
    • It initializes the output columns for device type and match type, which will be populated based on the campaign name.
  2. Function Definitions
    • Two functions, determine_device_type and determine_match_type, are defined to apply specific rules for determining the device and match types from the campaign name and type.
    • These functions convert campaign names to lowercase for case-insensitive matching and apply a series of conditional checks to assign the appropriate type.
  3. Data Filtering
    • The script filters the input dataset to exclude rows where the device type and match type are already populated, focusing only on rows that need processing.
  4. Applying Functions
    • The filtered dataset is processed by applying the defined functions to each row, updating the device and match type columns based on the campaign name and type.
  5. Output
    • The processed data is printed, showcasing the updated device and match type dimensions for the relevant campaigns.

Vitals

  • Script ID : 1351
  • Client ID / Customer ID: 1306913420 / 60268008
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Match Type Target, Device Target
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Kyle Perkins (kyle.perkins@genesys.com)
  • Created by Kyle Perkins on 2024-08-28 18:48
  • Last Updated by Kyle Perkins on 2024-08-28 19:12
> 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
72
73
74
75
76
77
78
79
##
## name: Dimension Update - Device & Match Type
## description:
##  Automates populating device and match type dimensions based on campaign name values.
## 
## author: 
## created: 2024-08-28
## 

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'  # Reference to the campaign type column
RPT_COL_DEVICE_TYPE = 'Device Target'  # Reference to the device type column
RPT_COL_MATCH_TYPE = 'Match Type Target'  # Reference to the match type column

# Output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_DEVICE_TYPE = 'Device Target'  # Output device type column
BULK_COL_MATCH_TYPE = 'Match Type Target'  # Output match type column

# Define the function to determine device type based on the provided rules
def determine_device_type(campaign_name):
    campaign_name = campaign_name.lower()  # Convert to lowercase to ignore case
    if campaign_name.endswith("_m"):
        return "M"
    elif "_dt_" in campaign_name:
        return "DT"
    elif "_m_" in campaign_name:
        return "M"
    elif campaign_name.endswith("_dt"):
        return "DT"
    elif "desktop" in campaign_name:
        return "DT"
    elif "mobile" in campaign_name:
        return "M"
    else:
        return "All"

# Define the function to determine match type based on the provided rules
def determine_match_type(campaign_name, campaign_type):
    campaign_name = campaign_name.lower()  # Convert to lowercase to ignore case
    campaign_type = campaign_type.lower()  # Convert campaign type to lowercase

    # Check if the campaign type is one of the specified types where Match Type should be "N/A"
    if campaign_type in ["display", "audience", "video", "discovery", "performance max"]:
        return "N/A"

    if "bmm" in campaign_name:
        return "BMM"
    elif "exact" in campaign_name:
        return "Exact"
    elif "phrase" in campaign_name:
        return "Phrase"
    elif "broad" in campaign_name:
        return "BMM"
    else:
        return "All"

# Filter inputDf to skip rows where device type and match type are already populated
filteredDf = inputDf[
    ~inputDf[RPT_COL_DEVICE_TYPE].notna() | ~inputDf[RPT_COL_MATCH_TYPE].notna()
].copy()

# Initialize outputDf so that it only includes rows that have been filtered
outputDf = filteredDf

# Apply the functions to each row in the filtered dataframe
outputDf[BULK_COL_DEVICE_TYPE] = filteredDf[RPT_COL_CAMPAIGN].apply(determine_device_type)
outputDf[BULK_COL_MATCH_TYPE] = filteredDf.apply(
    lambda row: determine_match_type(row[RPT_COL_CAMPAIGN], row[RPT_COL_CAMPAIGN_TYPE]), axis=1
)

# Printing the result
print(tableize(outputDf.head()))

Post generated on 2024-11-27 06:58:46 GMT

comments powered by Disqus