Script 1781: Script AutoTag Pacing Status #2

Purpose:

The Python script categorizes pacing percentages into descriptive tags such as “On Target” or “Over Pacing” based on predefined ranges.

To Elaborate

The script is designed to update a dimension called ‘Impr Pacing’ with descriptive tags that indicate the pacing status of a campaign. It evaluates the pacing percentage of each campaign and assigns a category based on specific thresholds. These thresholds are defined to determine whether a campaign is on target, under pacing, over pacing, significantly over pacing, or significantly under pacing. The script processes each pacing value, cleans it, and converts it to a percentage if necessary, before categorizing it according to the defined ranges. This categorization helps in quickly assessing the performance of campaigns relative to their pacing goals.

Walking Through the Code

  1. Configuration of Pacing Ranges:
    • The script begins by defining a dictionary PACING_RANGES that maps descriptive categories to specific percentage ranges. These ranges are user-configurable and determine how pacing values are categorized.
  2. Function Definition:
    • The categorize_pacing function is defined to process each pacing value. It cleans the input by removing any percentage signs and converts it to a float.
    • If the value is in decimal form (e.g., 1.24 instead of 124), it scales the value up by multiplying by 100.
    • The function iterates over the PACING_RANGES to find the appropriate category for the given pacing value and returns the category.
  3. Data Processing:
    • The script retrieves the primary data source and identifies relevant columns for processing.
    • It applies the categorize_pacing function to the ‘Pacing %’ column of the input data frame, updating the ‘Impr Pacing’ column in the output data frame with the categorized values.
  4. Output and Debugging:
    • The script includes print statements for debugging purposes, displaying the first few rows of the input data and the categorized pacing values.

Vitals

  • Script ID : 1781
  • Client ID / Customer ID: 1306928453 / 60270539
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Impr. Pacing
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: emerryfield@marinsoftware.com (emerryfield@marinsoftware.com)
  • Created by emerryfield@marinsoftware.com on 2025-03-06 18:08
  • Last Updated by emerryfield@marinsoftware.com on 2025-03-06 20:14
> 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
##
## name: Script AutoTag - Pacing Status
## description: Update the 'Impr Pacing' dimension with a worded value,
## such as on target/over pacing etc.
## The breakdown should be as follows:
## if value is betwen -1% & +5% , value output should be set as: 'On Target'
## if value is betwen -1% & -15% , value output should be set as: 'Under Pacing'
## if value is betwen +5% & +20% , value output should be set as: 'Over Pacing'
## if value is above +20% , value output should be set as: 'Significantly Over Pacing'
## if value is below -15% , value output should be set as: 'Significantly Under Pacing'##
##
## author: ChatGPT & Gregory Pantaine
## created: 2024-12-05
##
##
## name: Script AutoTag - Pacing Status
## description: Update the 'Impr Pacing' dimension with a worded value,
## such as on target/over pacing etc.
##
## author: ChatGPT & Gregory Pantaine
## created: 2024-12-05
##




# Configurable Pacing Range Definitions
PACING_RANGES = {
   'On Target': (99, 105),
   'Under Pacing': (85, 99),
   'Over Pacing': (105, 120),
   'Significantly Over Pacing': (120, float('inf')),
   'Significantly Under Pacing': (-float('inf'), 85)
##    'On Target': (-1, 5),
##    'Under Pacing': (-15, -1),
##    'Over Pacing': (5, 20),
##    'Significantly Over Pacing': (20, float('inf')),
##    'Significantly Under Pacing': (-float('inf'), -15)
}


def categorize_pacing(pacing_value):
    try:
        # Convert to string and remove '%' if present
        pacing_value_cleaned = str(pacing_value).replace('%', '').strip()
        
        # Convert to float and scale up if necessary
        pacing_percentage = float(pacing_value_cleaned)  # Convert to float

        # If values are in decimal form (e.g., 1.24 instead of 124), scale up
        if pacing_percentage < 10:  
            pacing_percentage *= 100  

        for category, (lower, upper) in PACING_RANGES.items():
            if lower <= pacing_percentage < upper:
                print(f"Value: {pacing_percentage}%, Category: {category}")  # Debug print
                return category
    except (ValueError, TypeError) as e:
        print(f"Error processing value {pacing_value}: {e}")  # Debug print
        pass
    return 'Undefined'  # Fallback for unexpected cases



today = datetime.datetime.now(CLIENT_TIMEZONE).date()


# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_PACING = 'Pacing %'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_IMPR_PACING = 'Impr. Pacing'


# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_CAMPAIGN_ID = 'Campaign ID'
BULK_COL_IMPR_PACING = 'Impr. Pacing'
outputDf[BULK_COL_IMPR_PACING] = "<<YOUR VALUE>>"


print(inputDf[RPT_COL_PACING].head())

# Pacing categorization
outputDf[BULK_COL_IMPR_PACING] = inputDf[RPT_COL_PACING].apply(categorize_pacing)


# Additional filtering or processing can be added here if needed
print(tableize(inputDf.head()))

Post generated on 2025-03-11 01:25:51 GMT

comments powered by Disqus