Last active
January 29, 2018 21:04
-
-
Save crow/5b8c36b2351ac31999e1a260d2222901 to your computer and use it in GitHub Desktop.
Provide Sample iOS and Android code for a custom event that is an install with IDFA or AAID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android: | |
In your MainActivity: | |
Call run on InstallEventTask | |
public static class InstallEventTask extends AsyncTask<Void, Void, Void> { | |
private static final String SHARED_PREFERENCE_FILE = "com.urbanairship.first_run"; | |
private static final String FIRST_RUN = "first_run"; | |
private final SharedPreferences sharedPreferences; | |
private final Context context; | |
public InstallEventTask(Context context) { | |
this.context = context.getApplicationContext(); | |
this.sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_FILE, Context.MODE_PRIVATE); | |
} | |
@Override | |
protected Void doInBackground(Void... voids) { | |
if (!sharedPreferences.getBoolean(FIRST_RUN, true)) { | |
return null; | |
} | |
String advertisingId = null; | |
boolean limitedAdTrackingEnabled = false; | |
switch (UAirship.shared().getPlatformType()) { | |
case UAirship.AMAZON_PLATFORM: | |
advertisingId = Settings.Secure.getString(context.getContentResolver(), "advertising_id"); | |
limitedAdTrackingEnabled = Settings.Secure.getInt(context.getContentResolver(), "limit_ad_tracking", -1) == 0; | |
break; | |
case UAirship.ANDROID_PLATFORM: | |
default: | |
if (!PlayServicesUtils.isGoogleAdsDependencyAvailable()) { | |
break; | |
} | |
try { | |
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); | |
if (adInfo == null) { | |
break; | |
} | |
advertisingId = adInfo.getId(); | |
limitedAdTrackingEnabled = adInfo.isLimitAdTrackingEnabled(); | |
} catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) { | |
Logger.error("Failed to generate first run event."); | |
break; | |
} | |
break; | |
} | |
CustomEvent.Builder eventBuilder = new CustomEvent.Builder("install"); | |
if (advertisingId != null) { | |
eventBuilder.addProperty("idfa", advertisingId) | |
.addProperty("limited_ad_tracking_enabled", limitedAdTrackingEnabled); | |
} | |
eventBuilder.create().track(); | |
sharedPreferences.edit() | |
.putBoolean(FIRST_RUN, false) | |
.apply(); | |
return null; | |
} | |
public static void run(Context context) { | |
new InstallEventTask(context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | |
} | |
} | |
iOS: | |
Add the following to your Appdelegate's didLaunchWithOptions: | |
Obj-C: | |
@import AdSupport; | |
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"com.urbanairship.install"] == false) { | |
[[NSUserDefaults standardUserDefaults] setValue:@"install event has been added" forKey:@"com.urbanairship.install"]; | |
UACustomEvent *event = [UACustomEvent eventWithName:@"install"]; | |
[event setStringProperty:[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] forKey:@"idfa"]; | |
[event setBoolProperty:[ASIdentifierManager sharedManager].isAdvertisingTrackingEnabled forKey:@"limited_ad_tracking_enabled"]; | |
[[UAirship shared].analytics addEvent:event]; | |
} | |
Swift: | |
import AdSupport | |
if (UserDefaults.standard.value(forKey: "com.urbanairship.install") != nil) { | |
UserDefaults.standard.setValue("install event has been added", forKey: "com.urbanairship.install") | |
let event = UACustomEvent(name: "install") | |
event.setStringProperty(ASIdentifierManager.shared().advertisingIdentifier.uuidString, forKey: "idfa") | |
event.setBoolProperty(ASIdentifierManager.shared().isAdvertisingTrackingEnabled, forKey: "limited_ad_tracking_enabled") | |
UAirship.shared().analytics.add(event) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment