ANDROID: find android advertising Id (aaid) and isLimitTrackingEnabled value
Working with Advertising IDs
Advertising ID is a user-resettable identifier and is appropriate for Ads use-cases, but there are some key points to bear in mind when using it:
Always respect the user’s intention in resetting the advertising ID. Do not bridge user resets by using a more persistent device identifier or fingerprint to link subsequent Advertising IDs together without the user’s consent. The Google Play Developer Content Policy states:
…upon reset, a new advertising identifier must not be connected to a previous advertising identifier or data derived from a previous advertising identifier without the explicit consent of the user
Always respect the associated Interest Based Advertising flag. Advertising IDs are configurable in that users can limit the amount of tracking associated with the ID. Always use the AdvertisingIdClient.Info.isLimitAdTrackingEnabled()
method to ensure you are not circumventing your users' wishes. The Google Play Developer Content Policy states:
…you must abide by a user’s ‘opt out of interest-based advertising’ setting. If a user has enabled this setting, you may not use the advertising identifier for creating user profiles for advertising purposes or for targeting users with interest-based advertising. Allowed activities include contextual advertising, frequency capping, conversion tracking, reporting and security and fraud detection.
API and class details :
Public class AdvertisingIdClient extends Object
Helper library for retrieval of advertising ID and related information such as the limit ad tracking setting.
It is intended that the advertising ID completely replace existing usage of other identifiers for ads purposes (such as use of ANDROID_ID
in Settings.Secure
) when Google Play Services is available. Cases where Google Play Services is unavailable are indicated by a GooglePlayServicesNotAvailableException
being thrown by getAdvertisingIdInfo().
public static AdvertisingIdClient.Info getAdvertisingIdInfo (Context context)
Retrieves the user’s advertising ID and limit ad tracking preference.
This method cannot be called in the main thread as it may block leading to ANRs. An IllegalStateException
will be thrown if this is called on the main thread.
Usage : Only use Advertising ID for user profiling or ads use-cases. When using an Advertising ID, always respect the Limit Ad Tracking flag, ensure the identifier cannot be connected to personally identifiable information (PII) and avoid bridging Advertising ID resets.
NOTE : to get more information on user Id
visit : https://developer.android.com/training/articles/user-data-ids.html
public class AdvertisingIDUtil { private static final String cAdvertisingIdClientName
= "com.google.android.gms.ads.identifier.AdvertisingIdClient";
private static final String cAdvertisingIdClientInfoName
= "com.google.android.gms.ads.identifier.AdvertisingIdClient$Info"; /**
* Starts an AsyncTask to retrieve and set the AAID.
* Does nothing if Settings.aaid is already set for the SDK.
*
* @param context context to retrieve the AAID on.
*/
@TargetApi(11)
public static void retrieveAndSetAAID(final Context context) {
new Thread() {
public void run(){
try {
if (context != null) {
// NPE catches null objects
Class<?> cInfo = Class.forName(cAdvertisingIdClientInfoName);
Class<?> cClient = Class.forName(cAdvertisingIdClientName); Method mGetAdvertisingIdInfo = cClient.getMethod("getAdvertisingIdInfo", Context.class);
Method mGetId = cInfo.getMethod("getId");
Method mIsLimitAdTrackingEnabled = cInfo.getMethod("isLimitAdTrackingEnabled"); Object adInfoObject = cInfo.cast(mGetAdvertisingIdInfo.invoke(null, context.getApplicationContext())); ColombiaSDKUtil.setAAID((String) mGetId.invoke(adInfoObject),
(Boolean) mIsLimitAdTrackingEnabled.invoke(adInfoObject));
}
} catch (ClassNotFoundException ignored) {
ignored.printStackTrace();
} catch (InvocationTargetException ignored) {
ignored.printStackTrace();
} catch (NoSuchMethodException ignored) {
ignored.printStackTrace();
} catch (IllegalAccessException ignored) {
ignored.printStackTrace();
} catch (ClassCastException ignored) {
ignored.printStackTrace();
} catch (NullPointerException ignored) {
ignored.printStackTrace();
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
}.start();
}