Script 829: Auto Tag Campaigns Funnel Stage

Purpose

Auto Tag the funnel stage of campaigns based on the campaign name.

To Elaborate

The Python script solves the problem of automatically tagging the funnel stage of campaigns based on the campaign name. It looks for specific keywords in the campaign name and assigns the corresponding funnel stage (Bottom, Middle, or Top) to each campaign. The script also handles cases where the campaign name does not contain the necessary keywords or if the funnel stage is already assigned correctly.

Walking Through the Code

  1. The script starts by defining the input dataframe as inputDf.
  2. It sets some configurable parameters such as the separator character (SEP) and the position of the funnel stage in the campaign name (TAG_LOCATION).
  3. Next, it defines the column names for the primary data source (RPT_COL_CAMPAIGN, RPT_COL_ACCOUNT, RPT_COL_FUNNEL_STAGE, RPT_COL_CAMPAIGN_STATUS) and the output columns (BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_FUNNEL_STAGE).
  4. The get_funnel_stage_from_campaign_name function is defined to extract the funnel stage from the campaign name using the separator and tag location.
  5. The script creates an output dataframe by copying the input dataframe.
  6. It then loops through each row in the input dataframe.
  7. For each row, it retrieves the existing funnel stage and campaign name.
  8. If the campaign name does not contain the separator, the script skips processing for that row.
  9. It calls the get_funnel_stage_from_campaign_name function to extract the funnel stage from the campaign name.
  10. The campaign name and funnel stage information are printed.
  11. If the extracted funnel stage is different from the existing funnel stage, it is assigned to the output dataframe.
  12. If the extracted funnel stage is None or the same as the existing funnel stage, it assigns NaN to the output dataframe.
  13. The script drops rows with empty funnel stages from the output dataframe.
  14. If the output dataframe is not empty, it is printed. Otherwise, a message indicating an empty output dataframe is printed.

Vitals

  • Script ID : 829
  • Client ID / Customer ID: 1306927165 / 60270223
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Funnel Stage
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
  • Created by dwaidhas@marinsoftware.com on 2024-03-20 14:48
  • Last Updated by dwaidhas@marinsoftware.com on 2024-03-22 19:06
> 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
68
69
70
##
## name: Auto Tag Campaigns - Funnel Stage
## description:
##  Auto Tag the funnel stage looking at the campaign name with Bottom, Middle, or Top 
## 
## author: Dana Waidhas 
## created: 2024-03-20
## 

inputDf = dataSourceDict["1"]

# Configurable Params
SEP = '_'
TAG_LOCATION = 3  # Comes after the third separator

# Primary data source and columns
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_FUNNEL_STAGE = 'Funnel Stage'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'

# Output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_FUNNEL_STAGE = 'Funnel Stage'

def get_funnel_stage_from_campaign_name(campaign_name):
    # Check if the separator is present in the campaign name
    if SEP in campaign_name:
        # Split by underscore
        parts = campaign_name.split(SEP)
        
        # Extract the word after the third underscore
        funnel_stage = parts[TAG_LOCATION] if len(parts) > TAG_LOCATION else None
        
        return funnel_stage
    else:
        # Return None if no separator is found
        return None

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

# Loop through all rows
for index, row in inputDf.iterrows():
    existing_funnel_stage = row[RPT_COL_FUNNEL_STAGE]
    campaign_name = row[RPT_COL_CAMPAIGN]
    
    # Skip processing if campaign name does not contain the separator
    if SEP not in campaign_name:
        continue

    funnel_stage = get_funnel_stage_from_campaign_name(campaign_name)

    # Print campaign and funnel stage information
    print("Campaign [%s] => Funnel Stage [%s]" % (campaign_name, funnel_stage))

    # Only tag if it's different than the existing funnel stage
    if funnel_stage is not None and funnel_stage != existing_funnel_stage:
        outputDf.at[index, BULK_COL_FUNNEL_STAGE] = funnel_stage
    else:
        outputDf.at[index, BULK_COL_FUNNEL_STAGE] = np.nan

# Only include non-empty funnel stages in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_FUNNEL_STAGE])

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