Script 955: Bidding Strategy Assignment

Purpose

Python script to automatically assign a campaign to an Impression Share Strategy called ‘Data Gathering’ for the first 7 days after being created, and then move it to a CPA strategy called ‘Performance Bidding’ after 7 days.

To Elaborate

The Python script solves the problem of automatically assigning campaigns to different bidding strategies based on their creation date. The script checks the creation date of each campaign and assigns it to the appropriate strategy. For campaigns created within the last 7 days, they are assigned to the “Data Gathering” strategy, and for campaigns older than 7 days, they are assigned to the “Performance Bidding” strategy.

The key business rules are as follows:

  • Campaigns created within 7 days are assigned to the “Data Gathering” strategy.
  • Campaigns older than 7 days are assigned to the “Performance Bidding” strategy.

The script takes an input DataFrame containing campaign data and outputs a modified DataFrame with the assigned strategies and folder check status.

Walking Through the Code

  1. The script starts by defining the current date using the datetime.now() function with the CLIENT_TIMEZONE parameter.
  2. The input DataFrame is obtained from a data source and stored in the inputDf variable.
  3. The script defines column constants for the input DataFrame.
  4. The process() function is defined to handle the main logic of assigning strategies.
  5. Inside the process() function, a copy of the input DataFrame is made and stored in the outputDf variable.
  6. The Campaign Creation Date column in the outputDf DataFrame is converted to datetime objects using the pd.to_datetime() function.
  7. A loop iterates over each row in the outputDf DataFrame.
  8. For each row, the script checks the number of days between the campaign creation date and the current date.
  9. If the number of days is less than or equal to 7, the campaign is assigned to the “Data Gathering” strategy and the Folder Check column is left blank.
  10. If the number of days is greater than 7, the campaign is assigned to the “Performance Bidding” strategy and the Folder Check column is set to “YES”.
  11. The modified outputDf DataFrame is printed for debugging purposes.
  12. The outputDf DataFrame is returned as the result of the process() function.
  13. The process() function is called with the inputDf DataFrame to obtain the final modified DataFrame.

Vitals

  • Script ID : 955
  • Client ID / Customer ID: 1306926011 / 69058
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Strategy, Folder Check
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jeremy Brown (jbrown@marinsoftware.com)
  • Created by Jeremy Brown on 2024-04-11 09:56
  • Last Updated by Jeremy Brown on 2024-04-11 09: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
##
## name: Bidding Strategy Assignment
## description:
##
## The Script automatically assigns a campaign to an Impression Share Strategy called 'Data Gathering' for the first 7 days after being created. Then after the campaign has been live for 7 days it is moved to a CPA strategy called 'Performance Bidding'. The logic is as follows;
## When a campaign's creation date is within 7 days of today's date, it is assigned to "Data Gathering" with a blank Folder Check column (""). Otherwise, it's assigned to "Performance Bidding" with a "YES" in the Folder Check column.
## 
## author: Jeremy Brown
## created: 2024-04-11
## 

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_PUBLISHER = 'Publisher'
RPT_COL_STRATEGY = 'Strategy'
RPT_COL_CAMPAIGN_CREATION_DATE = 'Campaign Creation Date'
RPT_COL_FOLDER_CHECK = 'Folder Check'
RPT_COL_IMPR = 'Impr.'

def process(inputDf):
    # Make a copy of the input DataFrame
    outputDf = inputDf.copy()
    
    # Get today's date in the specified timezone
    today_date = datetime.datetime.now(CLIENT_TIMEZONE).date()
    
    # Convert 'Campaign Creation Date' column to datetime objects (assuming format dd/mm/yyyy)
    outputDf['Campaign Creation Date'] = pd.to_datetime(outputDf['Campaign Creation Date'], format='%d/%m/%Y')
    
    # Determine the strategy and folder check based on the campaign creation date
    for index, row in outputDf.iterrows():
        creation_date = row['Campaign Creation Date'].date()
        if (today_date - creation_date).days <= 7:
            # Within 7 days of today's date
            outputDf.at[index, 'Strategy'] = "Data Gathering"
            outputDf.at[index, 'Folder Check'] = ""  # Leave Folder Check blank
        else:
            # More than 7 days old
            outputDf.at[index, 'Strategy'] = "Performance Bidding"
            outputDf.at[index, 'Folder Check'] = "YES"
    
    # Debugging: Print changes made to outputDf
    print("Data Changed:")
    print(outputDf[['Account', 'Campaign', 'Strategy', 'Folder Check']])
    
    return outputDf

# Process the DataFrame
outputDf = process(inputDf)

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

comments powered by Disqus