Script 25: Strategy Auto Assign on MTD Conv

Script 25: Strategy Auto Assign on MTD Conv

Purpose

Re-assign Strategy based on preset Month-to-Date Conversion levels

To Elaborate

The Python script aims to re-assign strategies to campaigns based on preset conversion ranges for the month-to-date. The script takes in a dataframe containing campaign data and applies conversion range rules to determine the new strategy for each campaign. It then identifies the campaigns with changed strategies and outputs the updated dataframe with the new strategies.

Walking Through the Code

  1. The script defines the conversion ranges for each strategy in the strategy_campaign_conversion_ranges dictionary.
  2. It sets column constants for various columns in the dataframe.
  3. A new column called ‘New Strategy’ is added to the input dataframe to track the new strategies.
  4. The script applies the conversion range rule for each strategy by iterating over the strategy_campaign_conversion_ranges dictionary.
  5. For each strategy, it checks if the campaign’s conversion falls within the specified range and assigns the corresponding strategy to the ‘New Strategy’ column.
  6. The script prints the campaigns with remapped strategies.
  7. The ‘New Strategy’ column is copied to the output dataframe.
  8. The output dataframe is filtered to include only campaigns with changed strategies.
  9. The script outputs the final dataframe with the updated strategies.

Vitals

  • Script ID : 25
  • Client ID / Customer ID: 309909744 / 14196
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Strategy
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Michael Huang (mhuang@marinsoftware.com)
  • Created by Michael Huang on 2023-03-21 09:55
  • Last Updated by Michael Huang on 2023-12-06 04:01
> 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
#
# Assign Campaign to Strategy according to Month-to-Date Conversions 
#
#
# Author: Michael S. Huang
# Date: 2023-01-20

# define strategy conversion ranges 
 # if over-lapping, last rule wins
strategy_campaign_conversion_ranges = {
    # key: strategy, value: (conv min, conv max)
    'Unassigned': (0, 1000),
    'Search - Non-Brand - Budget': (1000, 3000),
    'Search - Top Performers': (3000, 5000),
    'Full Funnel Bidding': (5000, 99999),
}



RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN_ID = 'Campaign ID'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_TYPE = 'Campaign Type'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_DAILY_BUDGET = 'Daily Budget'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_COST_PER_CONV = 'Cost/Conv. $'
RPT_COL_ROAS = 'ROAS'
RPT_COL_AVG_CPC = 'Avg. CPC $'
RPT_COL_CONV_RATE = 'Conv. Rate %'
RPT_COL_CTR = 'CTR %'
RPT_COL_CONV = 'Conv.'
RPT_COL_REVENUE = 'Revenue $'
RPT_COL_SEARCH_LOSTTOPISBUDGET = 'Search Lost Top IS (Budget) %'
RPT_COL_SEARCH_LOSTTOPISRANK = 'Search Lost Top IS (Rank) %'
RPT_COL_LOST_IMPRSHAREBUDGET = 'Lost Impr. Share (Budget) %'
RPT_COL_LOST_IMPRSHARERANK = 'Lost Impr. Share (Rank) %'
RPT_COL_AVG_BID = 'Avg. Bid $'
RPT_COL_HIST_QS = 'Hist. QS'
RPT_COL_IMPR = 'Impr.'
RPT_COL_CLICKS = 'Clicks'
RPT_COL_PUBLISHER = 'Publisher'
BULK_COL_STRATEGY = 'Strategy'

outputDf[BULK_COL_STRATEGY] = numpy.nan

# add new column to track new strategy
inputDf['New Strategy'] = numpy.nan

# Apply conversion range rule for each strategy
for strat, (conv_min, conv_max) in strategy_campaign_conversion_ranges.items():
    print("Applying Strategy Conv Range: ", strat, conv_min, conv_max)
    inputDf.loc[ (inputDf[RPT_COL_CONV] >= conv_min) & (inputDf[RPT_COL_CONV] <= conv_max), 'New Strategy'] = strat

# find changed strategies
print("== Campaigns with Remapped Strategy ==")
changed = inputDf[ inputDf['New Strategy'].notnull() & (inputDf[RPT_COL_STRATEGY] != inputDf['New Strategy']) ]
cols = [RPT_COL_CAMPAIGN, RPT_COL_CONV, RPT_COL_STRATEGY, 'New Strategy']
print(changed[cols].to_string())

# copy new strategy to output
outputDf.loc[:,RPT_COL_STRATEGY] = inputDf.loc[:,'New Strategy']

# only include campaigns with changed strategy in bulk file
outputDf = outputDf[ inputDf['New Strategy'].notnull() & (inputDf[RPT_COL_STRATEGY] != inputDf['New Strategy']) ]

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus