Script 1319: Scripts AutoTag Hotel Dimension

Purpose

The Python script parses campaign names to automatically tag hotel-related dimensions, handling special cases for specific account names.

To Elaborate

The script is designed to automate the process of tagging hotel-related dimensions based on campaign names. It specifically addresses the need to parse campaign names and assign appropriate tags for hotel locations, such as “YOTEL Amsterdam” or “YOTEL Boston,” based on specific substrings found within the campaign names. Additionally, it handles special cases where the account name includes “derbysoft,” modifying the campaign name accordingly. This ensures that the tagging is consistent and accurate, particularly for campaigns associated with the YOTEL brand. The script is useful for marketing teams or data analysts who need to categorize and manage hotel campaigns efficiently, ensuring that each campaign is tagged with the correct hotel location or brand name.

Walking Through the Code

  1. Configuration and Input Setup
    • The script begins by defining configurable parameters, such as the separator used in account names (SEP) and the index for tag location (TAG_LOCATION).
    • It retrieves input data from a data source dictionary, which is expected to contain campaign and account information.
  2. Tagging Function Definition
    • A function get_tag_from_account_name is defined to process each account and campaign name.
    • The function checks for specific substrings in the campaign name to determine the appropriate hotel tag, such as “AMN” for “YOTEL Amsterdam.”
    • Special handling is included for account names containing “derbysoft,” where the campaign name is modified by removing certain suffixes and ensuring proper capitalization.
  3. Data Processing and Output
    • The input data is copied to an output DataFrame, ensuring the necessary columns exist for tagging.
    • The tagging function is applied to each row of the DataFrame, assigning hotel tags based on the campaign and account names.
    • Rows with non-empty tags are retained, and the resulting DataFrame is printed or indicated as empty if no tags are assigned.

Vitals

  • Script ID : 1319
  • Client ID / Customer ID: 1306922797 / 60269073
  • Action Type: Bulk Upload (Preview)
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, HotelScriptTest
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-08-12 14:57
  • Last Updated by Grégory Pantaine on 2024-09-24 08:27
> 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
##
## Name: Campaign Name - Dimension Auto Tagging: Hotel
## Description:
##  Parse Campaign Name and add Campaign-level Marin Dimensions Tag for Hotel.
##  Handles special cases for "derbysoft" and "Brand" in Account names.
##
## author: Gregory Pantaine
## created: 2024-09-17
##
# Configurable Params - START
SEP = '_'  # Separator used in account names (if applicable)
TAG_LOCATION = 1  # Comes after the first separator (0-based index)
inputDf = dataSourceDict["1"]

# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_HOTEL = 'HotelScriptTest'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_HOTEL = 'HotelScriptTest'

def get_tag_from_account_name(account_name, campaign_name):
    print(f"Processing Account: {account_name}, Campaign: {campaign_name}")  # Debugging line

    # Specific location-based conditions
    if 'AMN' in campaign_name.lower():
        return "YOTEL Amsterdam"
    elif 'BOS' in campaign_name.lower():
        return "YOTEL Boston"
    elif 'CORE' in campaign_name.lower():
        return "YOTEL HQ"    
    elif 'EDI' in campaign_name.lower():
        return "YOTEL Edinburgh"
    elif 'GLA' in campaign_name.lower():
        return "YOTEL Glasgow"
    elif 'IST' in campaign_name.lower():
        return "YOTEL Istanbul"
    elif 'LCL' in campaign_name.lower():
        return "YOTEL London City"
    elif 'LGW' in campaign_name.lower():
        return "YOTEL London Gatwick"
    elif 'MIA' in campaign_name.lower():
        return "YOTEL Miami"
    elif 'MAN' in campaign_name.lower():
        return "YOTEL Manchester"
    elif 'NYC' in campaign_name.lower():
        return "YOTEL New York"
    elif 'CDG' in campaign_name.lower():
        return "YOTEL Paris CDG"
    elif 'POR' in campaign_name.lower():
        return "YOTEL Porto"
    elif 'SFA' in campaign_name.lower():
        return "YOTEL San Francisco"
    elif 'SIN' in campaign_name.lower():
        return "YOTEL Singapore"
    elif 'WDC' in campaign_name.lower():
        return "YOTEL Washington DC"
    elif 'AMS' in campaign_name.lower():
        return "YOTELAIR Amsterdam Schiphol"
    elif 'CHA' in campaign_name.lower():
        return "YOTELAIR Singapore Changi Airport"
    elif 'SRA' in campaign_name.lower():
        return "YOTELPAD London Stratford"

# Check if the account name contains "derbysoft"
    elif 'derbysoft' in account_name.lower():
        # Remove the suffix after the first space or dash
        campaign_name = re.sub(r' - \w+$', '', campaign_name)
        print(f"Modified Campaign Name (Derbysoft): {campaign_name}")  # Debugging line
        
        # Ensure "YOTEL" is all caps and the location is proper case
        if "YOTEL" in campaign_name.upper():
            words = campaign_name.split()
            words[0] = words[0].upper()
            campaign_name = ' '.join([words[0]] + [word.capitalize() for word in words[1:]])
        
        return campaign_name


    # Default case: return the modified account name
    else:
        return None # Skip returning the account name

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

# Ensure the output column exists before assigning
if BULK_COL_HOTEL not in outputDf.columns:
    outputDf[BULK_COL_HOTEL] = ""

# Apply the tagging function to each row, considering the special "derbysoft" and "Brand" cases
outputDf[BULK_COL_HOTEL] = outputDf.apply(
    lambda row: get_tag_from_account_name(row[BULK_COL_ACCOUNT], row[BULK_COL_CAMPAIGN]), axis=1
)

# Only include non-empty tags in bulk
outputDf = outputDf.dropna(subset=[BULK_COL_HOTEL])

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

# Configurable Params - END

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

comments powered by Disqus