This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def datetime_converter(value: datetime) -> str: | |
if isinstance(value, datetime): | |
return value.__str__() | |
def get_batch_size(record: dict) -> int: | |
batch = json.dumps({'items': record}, default=datetime_converter) | |
return floor(4000 / (sys.getsizeof(batch) / 1024)) | |
def import_data(clientid: str, clientsecret: str, | |
data_extension: str, data: list[dict) -> None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_access_token(clientid: str, clientsecret: str) -> str: | |
subdomain = 'YOUR_SUBDOMAIN' | |
auth_base_url = f'https://{subdomain}.auth.marketingcloudapis.com/v2/token' | |
headers = {'content-type': 'application/json'} | |
payload = { | |
'grant_type': 'client_credentials', | |
'client_id': clientid, | |
'client_secret': clientsecret | |
} | |
authentication_response = requests.post( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bs4 | |
import pandas as pd | |
import requests | |
def numeric_value(movie, tag, class_=None, order=None): | |
if order: | |
if len(movie.findAll(tag, class_)) > 1: | |
to_extract = movie.findAll(tag, class_)[order]['data-value'] | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def markov_chain_allocations(removal_effects, total_conversions): | |
re_sum = np.sum(list(removal_effects.values())) | |
return {k: (v / re_sum) * total_conversions for k, v in removal_effects.items()} | |
attributions = markov_chain_allocations(removal_effects_dict, total_conversions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removal_effects(df, conversion_rate): | |
removal_effects_dict = {} | |
channels = [channel for channel in df.columns if channel not in ['Start', | |
'Null', | |
'Conversion']] | |
for channel in channels: | |
removal_df = df.drop(channel, axis=1).drop(channel, axis=0) | |
for column in removal_df.columns: | |
row_sum = np.sum(list(removal_df.loc[column])) | |
null_pct = float(1) - row_sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transition_matrix(list_of_paths, transition_probabilities): | |
trans_matrix = pd.DataFrame() | |
list_of_unique_channels = set(x for element in list_of_paths for x in element) | |
for channel in list_of_unique_channels: | |
trans_matrix[channel] = 0.00 | |
trans_matrix.loc[channel] = 0.00 | |
trans_matrix.loc[channel][channel] = 1.0 if channel in ['Conversion', 'Null'] else 0.0 | |
for key, value in transition_probabilities.items(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transition_prob(trans_dict): | |
list_of_unique_channels = set(x for element in list_of_paths for x in element) | |
trans_prob = defaultdict(dict) | |
for state in list_of_unique_channels: | |
if state not in ['Conversion', 'Null']: | |
counter = 0 | |
index = [i for i, s in enumerate(trans_dict) if state + '>' in s] | |
for col in index: | |
if trans_dict[list(trans_dict)[col]] > 0: | |
counter += trans_dict[list(trans_dict)[col]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transition_states(list_of_paths): | |
list_of_unique_channels = set(x for element in list_of_paths for x in element) | |
transition_states = {x + '>' + y: 0 for x in list_of_unique_channels for y in list_of_unique_channels} | |
for possible_state in list_of_unique_channels: | |
if possible_state not in ['Conversion', 'Null']: | |
for user_path in list_of_paths: | |
if possible_state in user_path: | |
indices = [i for i, s in enumerate(user_path) if possible_state in s] | |
for col in indices: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_of_paths = df_paths['path'] | |
total_conversions = sum(path.count('Conversion') for path in df_paths['path'].tolist()) | |
base_conversion_rate = total_conversions / len(list_of_paths) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_paths = df.groupby('cookie')['channel'].aggregate( | |
lambda x: x.unique().tolist()).reset_index() | |
df_last_interaction = df.drop_duplicates('cookie', keep='last')[['cookie', 'conversion']] | |
df_paths = pd.merge(df_paths, df_last_interaction, how='left', on='cookie') | |
df_paths['path'] = np.where( | |
df_paths['conversion'] == 0, | |
['Start'] + df_paths['channel'] + ['Null'], | |
['Start'] + df_paths['channel'] + ['Conversion']) |
NewerOlder