Script 1203: Script Campaign Type de Campagne & Game

Purpose

The script extracts and categorizes campaign types and game names from campaign names in a dataset.

To Elaborate

The Python script is designed to parse campaign names from a dataset and extract two specific pieces of information: the campaign type and the game name. These pieces of information are then categorized into two separate dimensions, ‘Type de Campagne’ and ‘Game’. The script assumes that the campaign name follows a specific format where the ‘Type de Campagne’ appears before a vertical bar (‘|’) and the ‘Game’ appears after the bar but before a hyphen (‘-‘). The script processes each campaign name, extracts the relevant information, and populates new columns in the dataset with these values, ensuring that only rows with valid ‘Type de Campagne’ are retained in the final output.

Walking Through the Code

  1. Initialization and Configuration:
    • The script begins by defining constants for column names and creating a DataFrame from a data source.
    • A configurable parameter PLACEMENT_KEY is set to the vertical bar (‘ ’), which is used to split the campaign name.
  2. Function Definition:
    • A function get_game_and_campaign_type is defined to split the campaign name using the PLACEMENT_KEY.
    • It extracts the game name and campaign type based on the expected format and returns them.
  3. Data Processing:
    • The script copies the input DataFrame to an output DataFrame and initializes new columns for ‘Game’ and ‘Type de Campagne’ with NaN values.
    • It iterates over each row in the input DataFrame, checking if the campaign name contains the PLACEMENT_KEY.
    • If the key is present, it calls the function to extract the game and campaign type, updating the output DataFrame accordingly.
  4. Final Adjustments and Output:
    • The script filters the output DataFrame to include only rows where ‘Type de Campagne’ is not empty.
    • It removes any extra whitespace from the campaign names to ensure data consistency.
    • Finally, it prints the resulting DataFrame or indicates if it is empty.

Vitals

  • Script ID : 1203
  • Client ID / Customer ID: 1306927811 / 60270355
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Game, Type de Campagne
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Grégory Pantaine (gpantaine@marinsoftware.com)
  • Created by Grégory Pantaine on 2024-06-21 11:42
  • Last Updated by Grégory Pantaine on 2024-06-21 11:46
> 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
## name: Script - Campaign - Type de Campagne & Game
## description:
## Parse Campaign Name and pick out the campaign type into a dimension 'Type de campagne', same with game name into 'Game''.
##'Type de campagne' appears before the | and 'Game' is after the | but before the -.
## Created and adjusted by Grégory Pantaine with ChatGPT
## created: 2024-06-21


# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TYPE_CAMPAGNE = 'Type de Campagne'
RPT_COL_GAME = 'Game'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TYPE_CAMPAGNE = 'Type de Campagne'
BULK_COL_GAME = 'Game'

# Create a DataFrame
inputDf = dataSourceDict["1"]

########### Configurable Params - START ##########
PLACEMENT_KEY = '|'

# Output columns and initial values
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_TYPE_CAMPAGNE = 'Type de Campagne'
RPT_COL_GAME = 'Game'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_TYPE_CAMPAGNE = 'Type de Campagne'
BULK_COL_GAME = 'Game'

# Function to extract game and campaign type from campaign name
def get_game_and_campaign_type(campaign_name):
    parts = campaign_name.split('|')
    if len(parts) >= 2:
        game = parts[0].strip()
        type_parts = parts[1].strip().split(' - ')
        if len(type_parts) >= 2:
            campaign_type = type_parts[0].strip()
            return game, campaign_type
    return None, None

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

# Add new columns for 'Game' and 'Type de Campagne'
outputDf[BULK_COL_GAME] = np.nan
outputDf[BULK_COL_TYPE_CAMPAGNE] = np.nan

# Loop through all rows
for index, row in inputDf.iterrows():
    campaign_name = row[RPT_COL_CAMPAIGN]

    # Skip processing if campaign name does not contain the placement key
    if PLACEMENT_KEY not in campaign_name:
        continue

    game, campaign_type = get_game_and_campaign_type(campaign_name)

    # Only tag if they are found
    if game and campaign_type:
        outputDf.at[index, BULK_COL_GAME] = game
        outputDf.at[index, BULK_COL_TYPE_CAMPAGNE] = campaign_type

# Only include rows where 'Type de Campagne' is not empty
outputDf = outputDf.dropna(subset=[BULK_COL_TYPE_CAMPAGNE])

# Remove extra whitespace from campaign name that breaks Preview
outputDf[RPT_COL_CAMPAIGN] = outputDf[RPT_COL_CAMPAIGN].str.strip()

# Output the resulting DataFrame
if not outputDf.empty:
    print("outputDf", outputDf.head())
else:
    print("Empty outputDf")

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

comments powered by Disqus