package com.inepex.ineFrame.client.navigation;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class AnalyticsHandler implements PlaceChangedHandler {

	private boolean accountSet = false;
	
	@Inject
	public AnalyticsHandler(EventBus eventBus) {
		eventBus.addHandler(PlaceChangedEvent.TYPE, this);
	}

	@Override
	public void onPlaceChange(PlaceChangedEvent e) {
		if (!accountSet) {
			return;
		}
		trackPageview(e.getTokenWithoutParams());
	}
	
	public void setAccount(String accountId){
		accountSet = true;
		setAnalyticsAccount(accountId);
	}
	
	
	private native void setAnalyticsAccount(String accountId) /*-{
	$wnd._gaq.push([ '_setAccount', accountId ]);
	}-*/;
	
	public native void trackPageview(String page) /*-{
	$wnd._gaq.push([ '_trackPageview', page ]);
	}-*/;
}

@@ -244,23 +244,22 @@
 ate 
-boolean
+String
  account
 Set 
@@ -258,19 +258,46 @@
 ount
-Set = false
+Id;%0A%09private String universalAccountId
 ;%0A%09%0A
@@ -479,79 +479,291 @@
 if (
-!
 account
-Set) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09trackPageview(e.getTokenWithoutParams()
+Id != null) %09trackPageview(e.getTokenWithoutParams());%0A%09%09if (universalAccountId != null) trackUniversalPageview(e.getTokenWithoutParams());%0A%09%7D%0A%09%0A%09public void setUniversalAnalyticsAccount(String accountId)%7B%0A%09%09this.universalAccountId = accountId;%0A%09%09setUniversalAccount(accountId
 );%0A%09
@@ -815,25 +815,34 @@
 %7B%0A%09%09
+this.
 account
-Set = true
+Id = accountId
 ;%0A%09%09
@@ -1048,24 +1048,24 @@
  page) /*-%7B%0A
-
 %09$wnd._gaq.p
@@ -1104,10 +1104,228 @@
 %0A%09%7D-*/;%0A
+%09%0A%09private native void setUniversalAccount(String accountId) /*-%7B%0A%09$wnd.ga('create', accountId, 'auto');%0A%09%7D-*/;%0A%09%0A%09public native void trackUniversalPageview(String page) /*-%7B%0A%09$wnd.ga('send', 'pageview', page);%0A%09%7D-*/;%0A
 %7D%0A

handle universal analytics