Script 621: Pause CCT Groups

Purpose

The Python script automates the process of pausing specific groups in a campaign based on keyword and studio conditions.

To Elaborate

The script is designed to manage and automate the pausing of campaign groups within a marketing or advertising context. It processes input data to identify groups that need to be paused based on specific conditions related to keywords and studio names. The script checks if a keyword consists of a single word and pauses the group if this condition is met. Additionally, it checks if the group name contains the word “phrase” and if the studio is one of a predefined list, pausing the group if both conditions are satisfied. This ensures that only relevant groups are paused, optimizing the campaign management process by automating routine checks and updates.

Walking Through the Code

  1. Data Preparation
    • The script begins by defining the primary data source and relevant columns for processing.
    • It creates a copy of the input data for further manipulation.
  2. Keyword Check
    • A function check_token_length is defined to determine if a keyword is a single word, marking it as ‘PAUSED’ if true.
    • The script applies this function to the input data, updating the status for single-word keywords.
  3. Group Check
    • A function check_conditions is defined to check if the group name contains “phrase” and if the studio is in a specified list.
    • If both conditions are met, the group is marked as ‘PAUSED - ANIME’.
  4. Data Merging and Final Checks
    • The script creates a deduplicated group DataFrame and merges it with the keyword check results.
    • It updates the status and source template fields based on the checks, ensuring paused groups are correctly marked.
    • The script applies the anime check and updates the relevant fields accordingly.
  5. Output Preparation
    • The final merged DataFrame is prepared as the output, reflecting the updated statuses and conditions.

Vitals

  • Script ID : 621
  • Client ID / Customer ID: 1306924345 / 69058
  • Action Type: Bulk Upload
  • Item Changed: AdGroup
  • Output Columns: Account, Campaign, Group, Status, CCT_Single_keyword_processing, CCT_Group_pause_check, CCT_SourceTemplate_Backup, CCT_sourcetemplate
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jonathan Reichl (jreichl@marinsoftware.com)
  • Created by Jonathan Reichl on 2023-12-22 14:32
  • Last Updated by Jonathan Reichl on 2024-01-15 15:24
> 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
##
## name: Pause CCT Groups
## description:
##  
## 
## author: 
## created: 2023-12-22
## 

today = datetime.datetime.now(CLIENT_TIMEZONE).date()

# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_KEYWORD = 'Keyword'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_CCT_SOURCETEMPLATE = 'CCT_sourcetemplate'
RPT_COL_CCT_SINGLE_KEYWORD_PROCESSING = 'CCT_Single_keyword_processing'
RPT_COL_CCT_GROUP_PAUSE_CHECK = 'CCT_Group_pause_check'
RPT_COL_CCT_SOURCETEMPLATE_BACKUP = 'CCT_SourceTemplate_Backup'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_GROUP = 'Group'
RPT_COL_PUB_ID = 'Pub. ID'
RPT_COL_GROUP_PUB_ID = 'Group Pub. ID'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_STUDIO = 'Studio'
RPT_COL_GROUP_STATUS = 'Group Status'

# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_CCT_GROUP_PAUSE_CHECK = 'CCT_Group_pause_check'
BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING = 'CCT_Single_keyword_processing'
BULK_COL_CCT_SOURCETEMPLATE = 'CCT_sourcetemplate'
BULK_COL_CCT_SOURCETEMPLATE_BACKUP = 'CCT_SourceTemplate_Backup'
BULK_COL_STATUS_UPDATE = 'updated_status'
BULK_COL_STATUS = 'Status'
outputDf[BULK_COL_CCT_GROUP_PAUSE_CHECK] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SOURCETEMPLATE] = "<<YOUR VALUE>>"
outputDf[BULK_COL_CCT_SOURCETEMPLATE_BACKUP] = "<<YOUR VALUE>>"

# user code start here
print(tableize(inputDf))

def check_token_length(text):
    tokens = text.split()  # Splitting based on spaces
    if len(tokens) == 1:
        return 'PAUSED'
    else:
        return 'unchanged'

def check_conditions(df):
    # Check if 'RPT_COL_STUDIO' is in (x, y, z)
    studio_values = ['Medialink', 'Muse', 'Studio Pierrot', 'Liden Films']
    studio_check = df[RPT_COL_STUDIO].isin(studio_values)
    
    # Check if 'phrase' (case insensitive) is in 'RPT_COL_GROUP'
    phrase_check = df[RPT_COL_GROUP].str.contains('phrase', case=False, na=False)
    
    # If both conditions are met, return 'PAUSED - ANIME'
    df.loc[studio_check & phrase_check, 'anime_status'] = 'PAUSED - ANIME'


#Build keyword check DF 
check_columns = [BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP,RPT_COL_KEYWORD]
checkDf = inputDf[check_columns].copy()

#where only a single work set to puased 
checkDf[BULK_COL_STATUS_UPDATE] = checkDf[RPT_COL_KEYWORD].apply(check_token_length)


# set  BULK_COL_CCT_Single_word_check for paused objects 
checkDf.loc[checkDf[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = 'Paused|Single Word|'+ str(today)


print(tableize(checkDf))

# Filter checkDf to only include paused objects e.g. single keywords 
checkDf =  checkDf[checkDf[BULK_COL_STATUS_UPDATE] == 'PAUSED']


# build group DF 
group_columns = [BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP, RPT_COL_CCT_SOURCETEMPLATE, RPT_COL_GROUP_STATUS, RPT_COL_STUDIO] 
dedupeGroupDf = inputDf[group_columns].copy()

#one row per group 
dedupeGroupDf = dedupeGroupDf.drop_duplicates(subset=[BULK_COL_ACCOUNT,BULK_COL_CAMPAIGN,BULK_COL_GROUP], keep='first')


print('dedupeGroupDf')
print(tableize(dedupeGroupDf))

#merge 2 DFs 
merged_df = pd.merge(dedupeGroupDf, checkDf, on=[BULK_COL_ACCOUNT, BULK_COL_CAMPAIGN, BULK_COL_GROUP], how='left')
print('merged_df')
print(tableize(merged_df))

merged_df[BULK_COL_STATUS] = merged_df[RPT_COL_GROUP_STATUS]

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] != 'PAUSED' , BULK_COL_CCT_SINGLE_KEYWORD_PROCESSING] = 'Checked'

merged_df[BULK_COL_CCT_SOURCETEMPLATE_BACKUP] = merged_df[BULK_COL_CCT_SOURCETEMPLATE]

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_CCT_SOURCETEMPLATE] = ''

merged_df.loc[merged_df[BULK_COL_STATUS_UPDATE] == 'PAUSED' , BULK_COL_STATUS] = 'PAUSED'


#apply anime check
check_conditions(merged_df) 

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_CCT_SOURCETEMPLATE] = ''

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_STATUS] = 'PAUSED'

merged_df.loc[merged_df['anime_status'] == 'PAUSED - ANIME' , BULK_COL_CCT_GROUP_PAUSE_CHECK] = 'Paused|Anime|'+str(today)

merged_df.loc[merged_df['anime_status'] != 'PAUSED - ANIME' , BULK_COL_CCT_GROUP_PAUSE_CHECK] = 'Checked'


outputDf = merged_df

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

comments powered by Disqus