Script 739: Automate Campaigns DimensionTag Tactic

Purpose

Python script to set dimensions based on campaign name.

To Elaborate

The Python script aims to automate the process of setting dimensions for campaigns based on their names. It identifies specific keywords in the campaign names and assigns corresponding dimensions to them. The key business rules of this script are:

  • If the campaign name contains the keyword “KW-Defense” (case insensitive), the dimension “KW Defense” is assigned.
  • If the campaign name contains the keyword “PAT-Defense” (case insensitive), the dimension “PAT Defense” is assigned.
  • If the campaign name contains the keyword “SBV-PAT-Defense” (case insensitive), the dimension “SBV PAT Defense” is assigned.
  • If the campaign name contains the keyword “SD-Defense” (case insensitive), the dimension “SD Defense” is assigned.

Walking Through the Code

  1. The script defines column constants for various fields used in the code.
  2. The script initializes a temporary field name for storing the new tactic.
  3. The script blanks out the temporary field in the input dataframe.
  4. The script uses string matching to assign the new tactic based on the campaign name:
    • If the campaign name contains “KW-Defense”, the new tactic is set as “KW Defense”.
    • If the campaign name contains “PAT-Defense”, the new tactic is set as “PAT Defense”.
    • If the campaign name contains “SBV-PAT-Defense”, the new tactic is set as “SBV PAT Defense”.
    • If the campaign name contains “SD-Defense”, the new tactic is set as “SD Defense”.
  5. The script copies the new tactic to the output dataframe.
  6. The script filters the output dataframe to include only campaigns with a changed strategy (where the new tactic is not null and different from the existing tactic).

Vitals

  • Script ID : 739
  • Client ID / Customer ID: 1306927405 / 60270279
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Tactic
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-03-04 15:29
  • Last Updated by Grégory Pantaine on 2024-03-04 15:29
> 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
# Author: Gregory Pantaine
# Created: 2024-03-04
# Description: set dimensions based on campaign name
# Scripts name: Automate-Campaigns-DimensionTag-Tactic
# Report name (M1): Automate-Campaigns-SetDimensionTactic


RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_PUB_NAME = 'Publisher Name'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TACTIC = 'Tactic'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_PUB_NAME = 'Publisher Name'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TACTIC = 'Tactic'

#outputDf[BULK_COL_TACTIC] = "<<YOUR VALUE>>"

today = datetime.datetime.now(CLIENT_TIMEZONE).date()
print(tableize(inputDf))



TMP_FIELD = BULK_COL_TACTIC + '_new'
# blank out tmp field
inputDf[TMP_FIELD] = numpy.nan
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('KW-Defense', case=False)) , TMP_FIELD ] = 'KW Defense'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('PAT-Defense', case=False)) , TMP_FIELD ] = 'PAT Defense'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('SBV-PAT-Defense', case=False)) , TMP_FIELD ] = 'SBV PAT Defense'
inputDf.loc[ (inputDf[RPT_COL_CAMPAIGN].str.contains('SD-Defense', case=False)) , TMP_FIELD ] = 'SD Defense'

print(tableize(inputDf))

print(inputDf.index.duplicated())

# copy new strategy to output
outputDf.loc[:,BULK_COL_TACTIC] = inputDf.loc[:, TMP_FIELD]

# only include campaigns with changed strategy in bulk file
outputDf = outputDf[ inputDf[TMP_FIELD].notnull() & (inputDf[BULK_COL_TACTIC] != inputDf[TMP_FIELD]) ]

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

comments powered by Disqus