88from .config import TOKEN
99from .utils import parsers
1010
11+ controller = Controller .get_instance ()
1112mp = Mixpanel (TOKEN )
1213
1314
@@ -25,18 +26,32 @@ def get_default(team_name, user_id, project_name=None):
2526
2627class Trackable :
2728 TEAM_DATA = None
29+ INITIAL_EVENT = {"event_name" : "SDK init" , "properties" : {}}
30+ INITIAL_LOGGED = False
2831
29- def __init__ (self , function ):
32+ def __init__ (self , function , initial = False ):
3033 self .function = function
3134 self ._success = False
35+ self ._initial = initial
36+ if initial :
37+ self .track ()
3238 functools .update_wrapper (self , function )
3339
40+ @property
41+ def team (self ):
42+ return controller .get_team ()
43+
3444 def track (self , * args , ** kwargs ):
3545 try :
36- data = getattr (parsers , self .function .__name__ )(* args , ** kwargs )
46+ if self ._initial :
47+ data = self .INITIAL_EVENT
48+ Trackable .INITIAL_LOGGED = True
49+ self ._success = True
50+ else :
51+ data = getattr (parsers , self .function .__name__ )(* args , ** kwargs )
3752 event_name = data ["event_name" ]
3853 properties = data ["properties" ]
39- team_data = self .__class__ . TEAM_DATA .data
54+ team_data = self .team .data
4055 user_id = team_data .creator_id
4156 team_name = team_data .name
4257 properties ["Success" ] = self ._success
@@ -55,16 +70,20 @@ def track(self, *args, **kwargs):
5570
5671 def __call__ (self , * args , ** kwargs ):
5772 try :
58- self .__class__ .TEAM_DATA = Controller . get_instance () .get_team ()
59- ret = self .function (* args , ** kwargs )
73+ self .__class__ .TEAM_DATA = controller .get_team ()
74+ result = self .function (* args , ** kwargs )
6075 self ._success = True
6176 except Exception as e :
6277 self ._success = False
6378 raise e
6479 else :
65- return ret
80+ return result
6681 finally :
6782 try :
6883 self .track (* args , ** kwargs )
6984 except Exception :
7085 pass
86+
87+
88+ if __name__ == "lib.app.mixp.decorators" and not Trackable .INITIAL_LOGGED :
89+ Trackable (None , initial = True )
0 commit comments