#NEWLINE# def get_identity( context , prefix = None , identity_func = None , user = None ) : #NEWLINE# #INDENT# '\n    Get the identity of a logged in user from a template context.\n\n    The `prefix` argument is used to provide different identities to\n    different analytics services.  The `identity_func` argument is a\n    function that returns the identity of the user; by default the\n    identity is the username.\n    ' #NEWLINE# if ( prefix is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return context [ ( '%s_identity' % prefix ) ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# return identity_func [ 'analytical_identity' ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# if getattr ( settings , 'ANALYTICAL_AUTO_IDENTIFY' , True ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( user is None ) : #NEWLINE# #INDENT# user = get_user_from_context ( context ) #NEWLINE# #UNINDENT# if user . is_authenticated ( ) : #NEWLINE# #INDENT# if ( identity_func is not None ) : #NEWLINE# #INDENT# return identity_func ( user ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return user . get_username ( ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# return user . username #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except ( KeyError , AttributeError ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# return None