Skip to content

Instantly share code, notes, and snippets.

View MortenHegewald's full-sized avatar

Morten Hegewald MortenHegewald

View GitHub Profile
@MortenHegewald
MortenHegewald / smc_data_import.py
Last active July 28, 2020 01:46
Salesforce Marketing Cloud Data Import
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:
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(
@MortenHegewald
MortenHegewald / web_scrape_numeric_values.py
Created December 24, 2019 14:13
web scraper numeric values helper
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:
@MortenHegewald
MortenHegewald / attributions.py
Created November 19, 2019 02:24
MA - attributions
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)
@MortenHegewald
MortenHegewald / removal_effects.py
Created November 19, 2019 02:20
MA - Removal Effects
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
@MortenHegewald
MortenHegewald / trans_matrix.py
Last active November 17, 2019 21:34
MA - Transition Matrix
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():
@MortenHegewald
MortenHegewald / trans_prob.py
Last active November 17, 2019 20:12
MA - trans prob
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]]
@MortenHegewald
MortenHegewald / trans_states.py
Created November 17, 2019 19:44
MA - trans states
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:
@MortenHegewald
MortenHegewald / MA - Definitions.py
Last active November 17, 2019 19:40
MA Definitions
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)
@MortenHegewald
MortenHegewald / preprocessing.py
Created November 16, 2019 22:17
MA - Preprocess Data Set
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'])