Script 173: Strategy Targeting Type

Purpose

Python script to add a dimensions tag based on the campaign name.

To Elaborate

The Python script solves the problem of adding a dimensions tag to a campaign based on its name. The dimensions tag is extracted from the campaign name by splitting it using a separator and selecting the value after the first separator. The script then checks if the extracted tag is different from the existing tag in the input data. If it is different, the script updates the tag in the output data. The script also filters out rows with empty tags in the output data.

Walking Through the Code

  1. The configurable parameters are defined at the beginning of the script: SEP (separator used to split the campaign name) and TAG_LOCATION (position of the tag value after splitting).
  2. The column constants are defined: RPT_COL_CAMPAIGN, RPT_COL_ACCOUNT, RPT_COL_TEST, BULK_COL_ACCOUNT, and BULK_COL_CAMPAIGN.
  3. The get_tag_from_campaign_name function is defined to extract the tag from the campaign name by splitting it using the separator and selecting the value at the specified position.
  4. The input data is copied to the output data.
  5. A loop iterates through each row in the input data.
  6. The existing tag and campaign name for the current row are retrieved.
  7. The tag is extracted from the campaign name using the get_tag_from_campaign_name function.
  8. The campaign name and extracted tag are printed.
  9. If the extracted tag is not empty and different from the existing tag, the tag is updated in the output data.
  10. If the extracted tag is empty or the same as the existing tag, the tag in the output data is set to NaN.
  11. The output data is filtered to include only rows with non-empty tags.
  12. If the output data is not empty, it is printed in a table format.
  13. If the output data is empty, a message indicating an empty output is printed.

Vitals

  • Script ID : 173
  • Client ID / Customer ID: 1306924185 / 60269297
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Strategy // Targeting Type
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
  • Created by ascott@marinsoftware.com on 2023-06-07 17:28
  • 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#
# Add Dimensions Tag based on Campaign Name (minimal Pandas)
#
#
# Author: Michael S. Huang
# Date: 2023-05-17
#

########### Configurable Params - START ##########

SEP = '_'
TAG_LOCATION = 3 # comes after first separator

########### Configurable Params - END ###########

RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TEST = 'Strategy // Targeting Type'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TEST = 'Strategy // Targeting Type'

# define function to parse tag from campaign name
def get_tag_from_campaign_name(campaign_name):
    vals = campaign_name.split(SEP)
    # assume tag value is after 1st separator
    tag = ''
    try:
      tag = str(vals[TAG_LOCATION])
    except:
      tag = ''

    return tag

# copy all input rows to output
outputDf = inputDf.copy()

# loop through all rows
for index, row in inputDf.iterrows():
  existing_tag = row[RPT_COL_TEST]
  campaign_name = row[RPT_COL_CAMPAIGN]
  tag = get_tag_from_campaign_name(campaign_name)
  print("Campaign [%s] => Tag [%s]" % (campaign_name, tag))

  # only tag if it's different than existing tag
  if (len(tag) > 0) &  (tag != existing_tag):
    outputDf.at[index, BULK_COL_TEST] = tag
  else:
    outputDf.at[index, BULK_COL_TEST] = np.nan  

# only include non-empty tags in bulk
outputDf = outputDf.loc[ outputDf[BULK_COL_TEST].notnull() ]

if not outputDf.empty:
  print("outputDf", tableize(outputDf))
else:
  print("Empty outputDf")

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

comments powered by Disqus