Script 259: Dimension Update

Purpose

The Python script updates campaign dimension values based on specific patterns found in campaign names.

To Elaborate

The Python script is designed to update campaign dimension values by extracting specific patterns from campaign names. It focuses on two main dimensions: “Strategy//Targeting Type” and “Campaign Tactic.” The script uses regular expressions to identify patterns within campaign names that correspond to predefined codes or tactics. Once these patterns are identified, the script assigns the corresponding values to the respective dimensions. This process helps in organizing and categorizing campaigns based on their strategic and tactical attributes, which can be crucial for reporting and analysis purposes. The script also identifies campaigns with updated dimension values and prepares an output dataset containing only these modified campaigns.

Walking Through the Code

  1. Pattern Definition and Extraction
    • The script defines two regular expression patterns, PATTERN1 and PATTERN2, to identify specific codes and tactics within campaign names.
    • It extracts matches for these patterns from the campaign names and assigns them to the “Strategy//Targeting Type” and “Campaign Tactic” columns in the input DataFrame.
  2. Temporary Column Handling
    • A temporary column is created to facilitate the extraction of the “Campaign Tactic” by replacing certain substrings in the campaign names.
    • After extraction, this temporary column is deleted to clean up the DataFrame.
  3. Identifying and Handling Changes
    • The script checks for changes in the dimension values by comparing the extracted values to empty strings.
    • If changes are detected, it filters the DataFrame to include only the modified campaigns and prepares an output DataFrame with these updates. If no changes are found, an empty output DataFrame is prepared.

Vitals

  • Script ID : 259
  • Client ID / Customer ID: 1306922481 / 2
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Strategy//Targeting Type, Campaign Tactic
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jeremy Brown (jbrown@marinsoftware.com)
  • Created by Jeremy Brown on 2023-07-27 16:04
  • Last Updated by vapalussiere@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
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 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'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_STRATEGY_TARGETING_TYPE = 'Strategy-Targeting Type'
BULK_COL_CAMPAIGN_TACTIC = 'Campaign Tactic'

#outputDf[BULK_COL_STRATEGY_TARGETING_TYPE] = "TACTIC"
#outputDf[BULK_COL_CAMPAIGN_TACTIC] = "COST_MODEL"

# 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_STRATEGY_TARGETING_TYPE] = inputDf[RPT_COL_CAMPAIGN].str.extract(PATTERN1).fillna(' ')
inputDf['TEMP_COL'] = inputDf[RPT_COL_CAMPAIGN].str.replace('VID','CPV')

inputDf[RPT_COL_CAMPAIGN_TACTIC] =  inputDf['TEMP_COL'].str.extract(PATTERN2).fillna(' ')
del inputDf['TEMP_COL']
# define tmp column for dimension 'Strategy//Targeting Type' and set to empty
#TMP_TYPE = RPT_COL_STRATEGY_TARGETING_TYPE + '_'
#inputDf[TMP_TYPE] = np.nan

# define tmp column for dimension 'Campaign Tactic' and set to empty
#TMP_TACTIC = RPT_COL_CAMPAIGN_TACTIC + '_'
#inputDf[TMP_TACTIC] = np.nan

# Assign the matches to the respective dimensions based on matches for PATTERN1
#inputDf.loc[inputDf[RPT_COL_CAMPAIGN].apply(lambda x: bool(re.findall(PATTERN1, x))), TMP_TYPE] = MATCH1

# Assign the matches to the respective dimensions based on matches for PATTERN2
#inputDf.loc[inputDf[RPT_COL_CAMPAIGN].apply(lambda x: bool(re.findall(PATTERN2, x))), TMP_TACTIC] = MATCH2


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


# put changed campaigns into outputDf; if none, prepare empty outputDf
if sum(changed) > 0:
  print("== Campaigns with updated Dimension values ==", tableize(inputDf.loc[changed]))

  # only select changed rows
  #cols = [RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_STRATEGY_TARGETING_TYPE, RPT_COL_CAMPAIGN_TACTIC]
  #outputDf = inputDf.loc[ changed, cols ].copy() \
  #                  .rename(columns = { \
  #                    TMP_TYPE: BULK_COL_STRATEGY_TARGETING_TYPE, \
  #                    TMP_TACTIC: BULK_COL_CAMPAIGN_TACTIC \
  #                  })
  outputDf = inputDf.loc[changed]                 
  print("outputDf", tableize(outputDf))

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

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

comments powered by Disqus