package org.sagebionetworks.web.client.widget.mixpanel;


import com.google.inject.Inject;

/**
 * Wrapper around the MixPanel library
 * @author jayhodgson
 *
 */
public class MixPanelSdk {
	@Inject
	public MixPanelSdk() {
	}

	public void initialize(final String userId, final String email) {
		_initialize(userId, email);
	}

	private static native void _initialize(
			String userId,
			String synapseEmail) /*-{
		$wnd.mixpanel.identify(userId);
	    $wnd.mixpanel.register({
	        "email": synapseEmail
    	});
	    $wnd.mixpanel.track_links("#rootPanel a", "click link", 
	    	{"referrer": $doc.referrer}
	    );
	}-*/;
}

@@ -184,16 +184,44 @@
 elSdk %7B%0A
+%09String currentUser = null;%0A
 %09@Inject
@@ -322,39 +322,166 @@
 %7B%0A%09%09
-_initialize(userId, email);%0A%09%7D%0A
+if (!userId.equals(currentUser)) %7B%0A%09%09%09_changeUser(userId, email);%09%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void trackClick(String description) %7B%0A%09%09_track(%22click%22, description);%0A%09%7D%0A%09
 %0A%09pr
@@ -506,26 +506,26 @@
 e void _
-initialize
+changeUser
 (%0A%09%09%09Str
@@ -673,109 +673,170 @@
 %7D);%0A
-%09    $wnd.mixpanel.track_links(%22#rootPanel a%22, %22click link%22, %0A%09    %09%7B%22referrer%22: $doc.referrer%7D%0A%09    
+    %7D-*/;%0A%09%0A%09private static native void _track(String eventType, String eventDescription) /*-%7B%0A%09%09$wnd.mixpanel.track(eventType, %7B%22description%22 : eventDescription%7D
 );%0A%09

send track events