AndroidNativeUtil.getActivity().getApplication().getTracker()

23 views
Skip to first unread message

Gareth Murfin

unread,
Aug 21, 2016, 7:46:31 PM8/21/16
to CodenameOne Discussions

but getTracker() does not exist when I do this, 

Tracker t = AndroidNativeUtil.getActivity().getApplication().getTracker(TrackerName.APP_TRACKER);

How do I get around that ? Im thinking maybe all the google tracking stuff has updated on their end, but maybe not yours? 

It looks like some time ago I managed to get a tracker using this: 

GoogleAnalytics analytics = GoogleAnalytics.getInstance(AndroidNativeUtil.getActivity());
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        analytics.setLocalDispatchPeriod(1800);

        _("//// Start analytics for "+Prefs.GOOGLE_ANALYTICS_AGENT_ANDROID);
        Tracker t  = analytics.newTracker(Prefs.GOOGLE_ANALYTICS_AGENT_ANDROID);

I wonder if this is still safe to use?

Gareth Murfin

unread,
Aug 21, 2016, 8:24:25 PM8/21/16
to CodenameOne Discussions
ah of course you need to make getTracker().. Ended up doing it like this, got a compile will know later if it works:

// The following line should be changed to include the correct property id.
  private static final String PROPERTY_ID = Prefs.GOOGLE_ANALYTICS_AGENT_ANDROID;// "UA-XXXXX-Y";

  /**
   * Enum used to identify the tracker that needs to be used for tracking.
   *
   * A single tracker is usually enough for most purposes. In case you do need multiple trackers,
   * storing them all in Application object helps ensure that they are created only once per
   * application instance.
   */
  public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
  }

  HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
    
    synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

      GoogleAnalytics analytics = GoogleAnalytics.getInstance(AndroidNativeUtil.getActivity());

       Tracker t = analytics.newTracker(PROPERTY_ID);
      
      mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
  }
    
    public void doTracking() {
        
        // Get tracker.
        //getTracker() does not exist!!!
        Tracker t = getTracker(TrackerName.APP_TRACKER);
        
        // Set screen name.
        t.setScreenName("Splash");//i didnt know what to put in here so i put splash for now since it should launch the app thorugh that each time anyway

           t.send(new HitBuilders.ScreenViewBuilder()/*.setCampaignParamsFromUrl(campaignData)*/.build());
Reply all
Reply to author
Forward
0 new messages