import pandas as pd
from ChannelAttributionPro import *
password="mypassword"
df_aggr = pd.read_csv("https://app.channelattribution.io/data/data_aggregated.csv",sep=";")
print(df_aggr)
df_ctr = pd.read_csv("https://app.channelattribution.io/data/data_ctr.csv",sep=";")
print(df_ctr)
res=uam(df_aggr=df_aggr,df_ctr=df_ctr,df_paths=None,verbose=1,server="app.channelattribution.io",password=password)
print(res['attribution'])
df_aggr = pd.read_csv("https://app.channelattribution.io/data/data_aggregated.csv",sep=";")
print(df_aggr)
df_ctr = pd.read_csv("https://app.channelattribution.io/data/data_ctr.csv",sep=";")
print(df_ctr)
df_paths = pd.read_csv("https://app.channelattribution.io/data/data_paths.csv",sep=";")
print(df_paths)
res=uam(df_aggr=df_aggr,df_ctr=df_ctr,df_paths=df_paths,channel_conv_name="((CONV))",order=1,verbose=1,server="app.channelattribution.io",password=password)
print(res['attribution'])
df_paths_t=df_paths.copy()
df_paths_t['total_conversions']=0
df_paths_t.loc[df_paths_t['channel']=='((CONV))','total_conversions']=1
df_paths_t['total_conversions'] = df_paths_t.groupby('id_path')['total_conversions'].transform('sum')
df_paths_t=df_paths_t.loc[df_paths_t['channel']!='((CONV))']
df_paths_t = df_paths_t.groupby('id_path').agg({
'channel': lambda x: ' > '.join(x),
'total_conversions': 'first'
}).reset_index()
df_paths_t.rename(columns={'channel': 'path'},inplace=True)
res_path_attr=new_paths_attribution(Data=df_paths_t, var_path="path", var_conv="total_conversions", Dparams=res['parameters_mta'], var_value=None, row_sep=";", cha_sep=">", flg_write_nulls=1, flg_write_paths=0, server="app.channelattribution.io", password=password)
print(res_path_attr["attribution"])
df_aggr = pd.read_csv("https://app.channelattribution.io/data/data_aggregated_w_value.csv",sep=";")
print(df_aggr)
df_ctr = pd.read_csv("https://app.channelattribution.io/data/data_ctr.csv",sep=";")
print(df_ctr)
df_paths = pd.read_csv("https://app.channelattribution.io/data/data_paths.csv",sep=";")
print(df_paths)
channels=[x for x in df_aggr.columns if x not in ['timestamp_from','timestamp_to','conversions','value']]
res=uam(df_aggr=df_aggr[['timestamp_from','timestamp_to','conversions']+channels],df_ctr=df_ctr,df_paths=None,channel_conv_name="((CONV))",order=1,nsim_start=1e5,max_step=None,ncore=1,nfold=10,seed=1234567,conv_par=0.05,rate_step_sim=1.5,verbose=1,server="app.channelattribution.io",password=password)
res=res["attribution"]
res=pd.melt(res, id_vars=['timestamp_from','timestamp_to','conversions'], var_name='channel', value_name='attribution')
res=pd.merge(res,df_aggr[['timestamp_from','timestamp_to','value']],how='inner',on=['timestamp_from','timestamp_to'])
res['attribution_value']=res['value']*res['attribution']/res['conversions']
res=res.rename(columns={'attribution':'attribution_conversions'})
res=res[['timestamp_from','timestamp_to','conversions','value','channel','attribution_conversions','attribution_value']]
print(res)
df_aggr = pd.read_csv("https://app.channelattribution.io/data/data_aggregated.csv",sep=";")
print(df_aggr)
df_aggr_1=df_aggr.copy()
df_aggr_1["total_impressions"]=df_aggr_1["B"]+df_aggr_1["C"]
df_aggr_1["total_clicks"]=df_aggr_1["A"]+df_aggr_1["D"]+df_aggr_1["E"]+df_aggr_1["F"]
df_aggr_1=df_aggr_1[['timestamp_from','timestamp_to','conversions','total_impressions','total_clicks']]
print(df_aggr_1)
df_ctr = pd.read_csv("https://app.channelattribution.io/data/data_ctr.csv",sep=";")
del df_ctr['A']
df_ctr_1=pd.merge(df_aggr[['timestamp_from','timestamp_to','D','E','F']],df_ctr[['timestamp_from','timestamp_to','D','E','F']],on=['timestamp_from','timestamp_to'],how='outer')
df_ctr_1['sum_x']=df_ctr_1['D_x']+df_ctr_1['E_x']+df_ctr_1['F_x']
df_ctr_1['total_clicks']=df_ctr_1['D_y']*df_ctr_1['D_x']/df_ctr_1['sum_x']+df_ctr_1['E_y']*df_ctr_1['E_x']/df_ctr_1['sum_x']+df_ctr_1['F_y']*df_ctr_1['F_x']/df_ctr_1['sum_x']
df_ctr_1=df_ctr_1[['timestamp_from','timestamp_to','total_clicks']]
df_ctr_1['total_impressions']=np.nan
df_ctr_1=df_ctr_1[['timestamp_from','timestamp_to','total_impressions','total_clicks']]
print(df_ctr_1)
res_attr_1=uam(df_aggr=df_aggr_1,df_ctr=df_ctr_1,df_paths=None,channel_conv_name="((CONV))",order=1,nsim_start=1e5,max_step=None,ncore=1,nfold=10,seed=1234567,conv_par=0.05,rate_step_sim=1.5,verbose=1,server="app.channelattribution.io",password=password)
res_attr_1=res_attr_1['attribution']
print(res_attr_1)
df_paths = pd.read_csv("https://app.channelattribution.io/data/data_paths.csv",sep=";")
print(df_paths)
df_aggr_clicks=pd.merge(res_attr_1[['timestamp_from','timestamp_to','total_clicks']],df_aggr[["timestamp_from","timestamp_to","A","D","E","F"]],on=['timestamp_from','timestamp_to'],how='outer')
df_aggr_clicks=df_aggr_clicks.rename(columns={'total_clicks':'conversions'})
df_ctr_clicks=df_aggr_clicks[['timestamp_from','timestamp_to','conversions',"A","D","E","F"]]
df_ctr_clicks['A']=1
df_ctr_clicks['D']=1
df_ctr_clicks['E']=1
df_ctr_clicks['F']=1
del df_ctr_clicks['conversions']
res_attr_clicks=uam(df_aggr=df_aggr_clicks,df_ctr=df_ctr_clicks,df_paths=df_paths,channel_conv_name="((CONV))",order=1,nsim_start=1e5,max_step=None,ncore=1,nfold=10,seed=1234567,conv_par=0.05,rate_step_sim=1.5,verbose=1,server="app.channelattribution.io",password=password)
res_attr_clicks=res_attr_clicks['attribution']
del res_attr_clicks['conversions']
res_attr_clicks=res_attr_clicks.rename(columns={'A':'A_conversions'})
print(res_attr_clicks)
df_aggr_2=pd.merge(df_aggr,res_attr_clicks[["timestamp_from","timestamp_to","A_conversions"]],on=['timestamp_from','timestamp_to'],how='outer')
df_aggr_2['conversions']=df_aggr_2['conversions']-df_aggr_2['A_conversions']
df_aggr_2=df_aggr_2[['timestamp_from','timestamp_to','conversions','B','C','D','E','F']]
print(df_aggr_2)
res_attr=uam(df_aggr=df_aggr_2,df_ctr=df_ctr,df_paths=df_paths,channel_conv_name="((CONV))",order=1,nsim_start=1e5,max_step=None,ncore=1,nfold=10,seed=1234567,conv_par=0.05,rate_step_sim=1.5,verbose=1,server="app.channelattribution.io",password=password)
res_attr=res_attr['attribution']
del res_attr['conversions']
print(res_attr)
res_attr=pd.merge(res_attr_clicks[['timestamp_from','timestamp_to','A_conversions']],res_attr,on=['timestamp_from','timestamp_to'],how='outer')
res_attr=res_attr.rename(columns={'A_conversions':'A'})
res_attr=pd.merge(df_aggr[['timestamp_from','timestamp_to','conversions']],res_attr,on=['timestamp_from','timestamp_to'],how='outer')
print(res_attr)