package com.tapad.sample;

import android.app.Activity;
import android.os.Bundle;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.tapad.tapestry.*;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class GoogleAnalyticsActivity extends Activity {
    /**
     * TapestryAnalyticsService can be used from multiple Activities or the Application
     */
    public static class TapestryAnalyticsService {
        private static AtomicLong lastAnalyticsSend = new AtomicLong();

        private static void sendAnalytics(GoogleAnalyticsTracker tracker, Map<String, String> analytics) {
            if (!analytics.isEmpty()) {
                // You can modify the custom variables and scope here (2 = session-level scope)
                tracker.setCustomVar(1, "Visited Platforms", analytics.get("vp"), 2);
                tracker.setCustomVar(2, "Platforms Associated", analytics.get("pa"), 2);
                tracker.setCustomVar(3, "Platform Types", analytics.get("pt"), 2);
                tracker.setCustomVar(4, "First Visited Platform", analytics.get("fvp"), 2);
                // Because there can only be 5 custom variables the following is unused
                tracker.setCustomVar(6, "Most Recent Visited Platform", analytics.get("mrvp"), 2);
                if (analytics.get("movp") != null)
                    tracker.setCustomVar(5, "Most Often Visited Platform", analytics.get("movp"), 2);
                tracker.trackEvent("tapestry", "android", "", 0);
                tracker.dispatch();
                lastAnalyticsSend.set(System.currentTimeMillis());
            }
        }

        public static void track(final GoogleAnalyticsTracker tracker, TapestryClient tapestry) {
            boolean isNewSession = lastAnalyticsSend.get() < System.currentTimeMillis() - 30 * 60 * 1000;
            tapestry.send(new TapestryRequest().analytics(isNewSession), new TapestryCallback() {
                @Override
                public void receive(TapestryResponse response) {
                    sendAnalytics(tracker, response.analytics());
                }
            });
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
        tracker.startNewSession("UA-41283710-1", this);
        tracker.setDebug(true);
        TapestryAnalyticsService.track(tracker, new TapestryClient(this));
    }

    @Override
    protected void onStop() {
        super.onStop();
        GoogleAnalyticsTracker.getInstance().stopSession();
    }
}
@@ -19,17 +19,16 @@
 sample;%0A
-%0A
 import a
@@ -497,20 +497,20 @@
 nalytics
-Send
+Push
  = new A
@@ -608,17 +608,16 @@
 %3CString,
- 
 String%3E 
@@ -809,25 +809,25 @@
 (1, %22Visited
- 
+_
 Platforms%22, 
@@ -901,17 +901,17 @@
 latforms
- 
+_
 Associat
@@ -989,17 +989,17 @@
 Platform
- 
+_
 Types%22, 
@@ -1069,25 +1069,25 @@
 , %22First
- 
+_
 Visited
- 
+_
 Platform
@@ -1119,195 +1119,8 @@
 2);%0A
-                // Because there can only be 5 custom variables the following is unused%0A                tracker.setCustomVar(6, %22Most Recent Visited Platform%22, analytics.get(%22mrvp%22), 2);%0A
     
@@ -1219,23 +1219,23 @@
 Most
- 
+_
 Often
- 
+_
 Visited
- 
+_
 Plat
@@ -1399,20 +1399,20 @@
 nalytics
-Send
+Push
 .set(Sys
@@ -1612,12 +1612,12 @@
 tics
-Send
+Push
 .get
@@ -2322,16 +2322,25 @@
 ent(this
+, %2212345%22
 ));%0A    

add underscore