Script 265: Dimension Labelling Strategy Targeting Type AND Campaign Tactic v2

Purpose

Python script to assign campaign dimension values based on strategy/targeting type and campaign tactic.

To Elaborate

The Python script solves the problem of assigning campaign dimension values based on two factors: strategy/targeting type and campaign tactic. It takes input data and extracts relevant information from the campaign name to determine the strategy/targeting type and campaign tactic. The script then identifies any changes in the campaign dimension values and outputs the changed campaigns in a separate dataframe.

Walking Through the Code

  1. The script defines column constants for the campaign, account, strategy/targeting type, and campaign tactic.
  2. It defines two pattern match variables for identifying specific patterns in the campaign name.
  3. The script extracts the campaign tactic from the campaign name using the first pattern match and fills any missing values with a space.
  4. It creates a temporary column and replaces “VID” with “CPV” in the campaign name, then extracts the strategy/targeting type using the second pattern match and fills any missing values with a space.
  5. The temporary column is removed.
  6. The script identifies any changed campaigns by checking if the strategy/targeting type or campaign tactic is not equal to a space.
  7. It selects only the relevant columns from the input dataframe.
  8. If there are any changed campaigns, the script outputs the changed campaigns in a separate dataframe.
  9. If there are no changed campaigns, an empty dataframe is prepared.
  10. The output dataframe is printed.

Vitals

  • Script ID : 265
  • Client ID / Customer ID: 1306924185 / 60269297
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Campaign Tactic, Strategy // Targeting Type
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
  • Created by ascott@marinsoftware.com on 2023-07-28 14:34
  • Last Updated by ascott@marinsoftware.com 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
#
# Assign Campaign Dimension Values according to:
#  - Strategy//Targeting Type
#  - Campaign Tactic
#
# Author: Jeremy Brown
# Date: 2023-07-27

RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STRATEGY_TARGETING_TYPE = 'Strategy // Targeting Type'
RPT_COL_CAMPAIGN_TACTIC = 'Campaign Tactic'

# define pattern match
PATTERN1 = r'(ISA|ISG|AWR|TRF|YTB|IGR|TIK|YTS)'
PATTERN2 = r'(?:CPV_)?(CPM|CPV|CPC)'

# find all occurrences of the pattern in the Campaign name
inputDf[RPT_COL_CAMPAIGN_TACTIC] = inputDf[RPT_COL_CAMPAIGN].str.extract(PATTERN1).fillna(' ')

# Transform VID to CPV (as it's the expected nomenclature) into a temp col, extract patter from said new col then remove it (superfluous here but kept for ref)
inputDf['TEMP_COL'] = inputDf[RPT_COL_CAMPAIGN].str.replace('VID','CPV')
inputDf[RPT_COL_STRATEGY_TARGETING_TYPE] =  inputDf['TEMP_COL'].str.extract(PATTERN2).fillna(' ')
del inputDf['TEMP_COL']


# find changed campaigns
changed = inputDf[RPT_COL_STRATEGY_TARGETING_TYPE].ne(' ') | inputDf[RPT_COL_CAMPAIGN_TACTIC].ne(' ')

#selecting only relevant cols
inputDf= inputDf[[RPT_COL_CAMPAIGN,RPT_COL_ACCOUNT,RPT_COL_STRATEGY_TARGETING_TYPE,RPT_COL_CAMPAIGN_TACTIC]]

# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
  #print('changed data',tableize(inputDf[changed]))
  outputDf = inputDf[changed]                 
  print("outputDf", tableize(outputDf))

else:
  print("Empty outputDf")
  outputDf = outputDf.iloc[0:0]

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

comments powered by Disqus