Script 1137: Script Campaign CPA LW vs TQuarter

Purpose

The script compares the Cost Per Acquisition (CPA) for campaigns between the current quarter and the last week, flagging those with significant deviations.

To Elaborate

The Python script is designed to analyze marketing campaign performance by comparing the Cost Per Acquisition (CPA) metrics over two different time periods: the current quarter and the last week. It identifies campaigns where the CPA for the last week is significantly different—specifically, 30% higher or lower—than the CPA for the current quarter. This helps in quickly identifying campaigns that may require attention due to unexpected performance changes, allowing for timely adjustments in marketing strategies. The script processes data from a provided DataFrame, calculates thresholds for flagging significant CPA deviations, and outputs a list of campaigns that meet these criteria.

Walking Through the Code

  1. Data Preparation
    • The script begins by assuming that a DataFrame, inputDf, is provided within a dictionary named dataSourceDict. This DataFrame contains the necessary data for analysis, including columns for campaign names, account names, and CPA metrics for both the current quarter and the last week.
  2. Threshold Calculation
    • It calculates two thresholds for each campaign: Threshold_High and Threshold_Low. These thresholds are set at 30% above and below the quarterly CPA, respectively. This is done to determine the acceptable range of CPA variation.
  3. Flagging Campaigns
    • The script uses these thresholds to flag campaigns where the last week’s CPA is either above the Threshold_High or below the Threshold_Low. This is achieved using a conditional check with NumPy’s np.where function, which adds a ‘Flag’ to campaigns that meet the criteria.
  4. Output
    • Finally, the script filters the DataFrame to include only the flagged campaigns and prints this filtered DataFrame in a table format using a custom tableize function. This provides a clear view of which campaigns require further investigation.

Vitals

  • Script ID : 1137
  • Client ID / Customer ID: 1306927809 / 60270355
  • Action Type: Email Report
  • Item Changed: None
  • Output Columns:
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-05-23 13:44
  • Last Updated by Grégory Pantaine on 2024-05-23 13:52
> 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
## name: Script - Campaign - CPA LW vs TQuarter
## description:
## Compare CPA for the current quarter with the CPA for the last week for campaigns and flag those with a CPA for LW 30% higher or lower than the quarterly CPA.
## Author: Gregory Pantaine
## created: 2024-05-23
##

# Simulated CLIENT_TIMEZONE for example
CLIENT_TIMEZONE = datetime.timezone.utc

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

# Assuming `dataSourceDict` is provided and contains a DataFrame with the necessary data
# Primary data source and columns
inputDf = dataSourceDict["1"]

# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CPA_QUARTER = 'Campaign CPA This Quarter'
RPT_COL_CPA_LW = 'All - Complete Registration - CPA'

# primary data source and columns
inputDf = dataSourceDict["1"]

# Calculate the threshold for flagging (30% higher or lower)
inputDf['Threshold_High'] = inputDf[RPT_COL_CPA_QUARTER] * 1.30
inputDf['Threshold_Low'] = inputDf[RPT_COL_CPA_QUARTER] * 0.70

# Create a new column to flag the campaigns that meet the criteria
inputDf['Flag'] = np.where((inputDf[RPT_COL_CPA_LW] > inputDf['Threshold_High']) | 
                           (inputDf[RPT_COL_CPA_LW] < inputDf['Threshold_Low']), 'Flag', '')

# Output the flagged campaigns
flagged_campaigns = inputDf[inputDf['Flag'] == 'Flag']

# Function to print the DataFrame as a table (for the purpose of this example)
def tableize(df):
    return df.to_string(index=False)

# Print the tableized version of the flagged campaigns DataFrame
print(tableize(flagged_campaigns))

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

comments powered by Disqus