Android Open Source - snakk-ads-android-sample-app Event Tracker






From Project

Back to project page snakk-ads-android-sample-app.

License

The source code is released under:

Copyright (c) 2012, Snakk! Media Group All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project snakk-ads-android-sample-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.snakk.adview.track;
//from   www. jav  a2s  . co m
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;

import com.snakk.adview.AdLog;
import com.snakk.adview.Utils;

/**
 * Application Event Tracker. Sends a notification to PW servers with UDID,
 * UA, and Package Name
 */
public class EventTracker {
  private Context mContext;
  private String mPackageName;
  private String mEvent;
  private String ua = null;

  static private String TRACK_HOST = "a.snakkads.com";
  static private String TRACK_HANDLER = "/trackevent.php";

  private static EventTracker mInstance = null;
  
  private AdLog adLog = new AdLog(this);

  private EventTracker() {
    super();
  }

  public static EventTracker getInstance() {
    if (mInstance == null) {
      mInstance = new EventTracker();
    }
    return (mInstance);
  }

  /**
   * Send Event Notification To Phunware
   * 
   * @param context
   *            - The reference to the context of Activity
   * 
   * @param event
   *            - The tag string of the event that occurred
   */
  public void reportEvent(Context context, String event) {
    if (context == null) {
      return;
    }

    mContext = context;
    mEvent = event;
    mPackageName = mContext.getPackageName();
    if (ua == null)
      ua = Utils.getUserAgentString(mContext);

    new Thread(mTrackEvent).start();
  }

  private Runnable mTrackEvent = new Runnable() {
    public void run() {
      StringBuilder sz = new StringBuilder("http://" + TRACK_HOST + TRACK_HANDLER);
      sz.append("?pkg=" + mPackageName);

      if (mEvent == null || mEvent == "") {
        adLog.log(AdLog.LOG_LEVEL_1, AdLog.LOG_TYPE_ERROR, "EventTracker", "Event track failed: No Event Tag Defined");
        return;
      }

      try {
        sz.append("&event=" + URLEncoder.encode(mEvent, "UTF-8"));
      } catch(Exception e){
      }

      // Lookup UDID
      String deviceIdMD5 = Utils.getDeviceIdMD5(mContext);
      sz.append("&udid=" + deviceIdMD5);

      // User Agent
      //
      if (ua != null) {
        try {
          sz.append("&ua=" + URLEncoder.encode(ua, "UTF-8"));
        } catch (Exception e) {
        }
      }

      String url = sz.toString();
      adLog.log(AdLog.LOG_LEVEL_3, AdLog.LOG_TYPE_INFO, "EventTracker", "Event track: " + url);

      DefaultHttpClient httpclient = new DefaultHttpClient();
      HttpGet httpget = new HttpGet(url);
      HttpResponse response;
      try {
        response = httpclient.execute(httpget);
      } catch (ClientProtocolException e) {
        // Just fail silently. We'll try the next time the app opens
        adLog.log(AdLog.LOG_LEVEL_1, AdLog.LOG_TYPE_ERROR, "EventTracker", "Event track failed: ClientProtocolException (no signal?)");
        return;
      } catch (IOException e) {
        // Just fail silently. We'll try the next time the app opens
        adLog.log(AdLog.LOG_LEVEL_1, AdLog.LOG_TYPE_ERROR, "EventTracker", "Event track failed: IOException (no signal?)");
        return;
      }

      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        adLog.log(AdLog.LOG_LEVEL_1, AdLog.LOG_TYPE_ERROR, "EventTracker", "Event track failed: Status code != 200");
        return;
      }

      /* TODO: remove:
      HttpEntity entity = response.getEntity();
      if (entity == null || entity.getContentLength() == 0) {
        adLog.log(AdLog.LOG_LEVEL_1, AdLog.LOG_TYPE_ERROR, "EventTracker", "Event track failed: Response was empty");
        return;
      }
      */

      // If we made it here, the request has been tracked
      adLog.log(AdLog.LOG_LEVEL_2, AdLog.LOG_TYPE_INFO, "EventTracker", "Event track successful");
    }
  };

  /**
   * Set log level:<br>
   * {@link com.snakk.adview.AdLog#LOG_LEVEL_NONE}<br>
   * {@link com.snakk.adview.AdLog#LOG_LEVEL_1}<br>
   * {@link com.snakk.adview.AdLog#LOG_LEVEL_2}<br>
   * {@link com.snakk.adview.AdLog#LOG_LEVEL_3}<br>
   *
   * @param logLevel
   */
  public void setLogLevel(int logLevel) {
    adLog.setLogLevel(logLevel);
  }
}




Java Source Code List

com.snakk.advertising.Config.java
com.snakk.advertising.SnakkAdPrompt.java
com.snakk.advertising.SnakkAdRequest.java
com.snakk.advertising.SnakkAdvertising.java
com.snakk.advertising.SnakkBannerAdView.java
com.snakk.advertising.SnakkInterstitialAd.java
com.snakk.advertising.SnakkVideoInterstitialAd.java
com.snakk.advertising.internal.AbstractStatefulAd.java
com.snakk.advertising.internal.AdActivityContentWrapper.java
com.snakk.advertising.internal.AdPromptImpl.java
com.snakk.advertising.internal.AdRequestImpl.java
com.snakk.advertising.internal.BasicWebView.java
com.snakk.advertising.internal.DeviceCapabilities.java
com.snakk.advertising.internal.InterstitialAdImpl.java
com.snakk.advertising.internal.InterstitialBaseView.java
com.snakk.advertising.internal.Sharable.java
com.snakk.advertising.internal.SnakkAdActivity.java
com.snakk.advertising.internal.VastPlayerView.java
com.snakk.advertising.internal.VideoInterstitialAdImpl.java
com.snakk.advertising.package-info.java
com.snakk.adview.AdFullscreenView.java
com.snakk.adview.AdInterstitialBaseView.java
com.snakk.adview.AdInterstitialView.java
com.snakk.adview.AdLog.java
com.snakk.adview.AdOfferWallView.java
com.snakk.adview.AdPrompt.java
com.snakk.adview.AdRequest.java
com.snakk.adview.AdVideoUnitView.java
com.snakk.adview.AdViewCore.java
com.snakk.adview.AdView.java
com.snakk.adview.AutoDetectedParametersSet.java
com.snakk.adview.Base64.java
com.snakk.adview.DeprecatedInterstitialBaseView.java
com.snakk.adview.IMraidCommand.java
com.snakk.adview.LockedOrientationDialog.java
com.snakk.adview.MraidCommand.java
com.snakk.adview.Mraid.java
com.snakk.adview.OldAdActivity.java
com.snakk.adview.Rotate3dAnimation.java
com.snakk.adview.Utils.java
com.snakk.adview.track.EventTracker.java
com.snakk.adview.track.InstallTracker.java
com.snakk.adview.package-info.java
com.snakk.core.SnakkLog.java
com.snakk.vastsdk.TVASTAdErrorEvent.java
com.snakk.vastsdk.TVASTAdErrorListener.java
com.snakk.vastsdk.TVASTAdError.java
com.snakk.vastsdk.TVASTAdType.java
com.snakk.vastsdk.TVASTAdView.java
com.snakk.vastsdk.TVASTAd.java
com.snakk.vastsdk.TVASTAdsLoader.java
com.snakk.vastsdk.TVASTAdsRequest.java
com.snakk.vastsdk.TVASTCompanionAdSlot.java
com.snakk.vastsdk.TVASTCompanionAd.java
com.snakk.vastsdk.TVASTCreative.java
com.snakk.vastsdk.TVASTLinearAd.java
com.snakk.vastsdk.TVASTLinearIcon.java
com.snakk.vastsdk.TVASTMediaFile.java
com.snakk.vastsdk.TVASTNonlinearAd.java
com.snakk.vastsdk.TVASTPostbackTask.java
com.snakk.vastsdk.TVASTUtils.java
com.snakk.vastsdk.TVASTVideoAdsManager.java
com.snakk.vastsdk.player.TVASTPlayer.java
com.snakk.vastsdk.player.TVASTSharable.java
com.snakk.vastsdk.player.TVASTTrackingVideoView.java
com.snakk.vastsdk.player.TVASTVideoProgressThread.java
com.snakkads.tabsswipe.BannerAdPromptFragment.java
com.snakkads.tabsswipe.BannerInterstitialFragment.java
com.snakkads.tabsswipe.BannerRichMediaFragment.java
com.snakkads.tabsswipe.BannerStandardMediaFragment.java
com.snakkads.tabsswipe.BannerVideoFragment.java
com.snakkads.tabsswipe.MainActivity.java
com.snakkads.tabsswipe.SplashScreen.java
com.snakkads.tabsswipe.adapter.TabsPagerAdapter.java
com.yourcompany.AdMobActivity.java
com.yourcompany.SampleActivity.java