1page.title=Advertising ID 2page.tags=Ads,Advertising ID,ID 3header.hide=1 4 5@jd:body 6 7<div class="landing-banner"> 8 9<div class="col-8"> 10 11 <h1 itemprop="name" style="margin-bottom:0;">Advertising ID</h1> 12 <p itemprop="description">The advertising ID is a user-specific, unique, 13 resettable ID for advertising, provided by Google Play services. It gives 14 users better controls and provides developers with a simple, standard system 15 to continue to monetize your apps. It is an anonymous identifier for advertising 16 purposes and enables users to reset their identifier or opt out of interest-based 17 ads within Google Play apps. </p> 18 19<p>The advertising ID is accessible through a straightforward API that you can implement in your apps. For details, 20take a look at the 21<a href="#get_started">overview</a> and the <a 22href="{@docRoot}reference/com/google/android/gms/ads/identifier/package-summary.html">advertising ID API reference</a>. 23 24</div> 25</div> 26 27<div class="landing-docs"> 28 <div class="col-6 normal-links"> 29 <h3 style="clear:left">Key Developer Features</h3> 30 31 <h4>Standard, simple ID</h4> 32 <p>The advertising ID is a part of a standard, simple system for serving ads and performing analytics.</p> 33 34 <h4>Giving users control</h4> 35 <p>Users can reset their advertising ID or opt out of interest-based ads at any time, right from the Google Settings app. 36 Their preferences apply across all ad companies that use the advertising ID.</p> 37 38 </div> 39 40 <div class="col-6 normal-links"> 41 <h3 style="clear:left">Getting Started</h3> 42 <h4>1. Get the Google Play services SDK</h4> 43 <p>The advertising ID APIs are part of the Google Play services platform.</p> 44 <p>To get started, <a href="{@docRoot}google/play-services/setup.html">set up 45 the Google Play services SDK</a>. </p> 46 47 <h4>2. Read the docs and example code</h4> 48 <p>Once you've installed the Google Play services package, review the <a href="#get_started">overview</a> 49 below, as well as the <a href="#example">example</a>.</p> 50 <p> 51 For detailed documentation, take a look at the <a href= 52 "{@docRoot}reference/com/google/android/gms/ads/identifier/package-summary.html"> 53 advertising ID API reference documentation</a>. 54 </p> 55 </div> 56</div> 57 58<p class="caution" style= 59"background-color:#fffdeb;width:100%;margin-bottom:1em;padding:.5em;"> 60 As a reminder, please note that starting <strong>1 August 2014</strong>, new 61 apps and app updates distributed through Google Play must use the advertising 62 ID in lieu of any other persistent identifiers for any advertising purposes, 63 on devices that support the advertising ID.<br> 64 <br> 65 To learn how to check your app's compliance through the Developer Console, or 66 for details on the associated developer policy changes, please see the 67 <a href= 68 "https://support.google.com/googleplay/android-developer/answer/6048248">Advertising 69 ID topic</a> in the Google Play developer help center. 70</p> 71 72<h2 id="get_started">Using the Advertising ID</h2> 73 74<p> 75 The <strong>advertising ID</strong> is a unique but 76 user-resettable string identifier that lets ad networks and other apps anonymously 77 identify a user. The user's advertising ID is made available to apps through APIs 78 provided in Google Play services. 79</p> 80<p> 81 Users can reset their advertising ID at any time, right from the Ads section of the 82 Google Settings app on their devices. From the same app, users can also 83 opt-out of targeted advertising based on the advertising ID by setting the appropriate 84 <strong style="white-space:nowrap">ad tracking preference</strong>. When the 85 user opts-out of targeted ads, this ad tracking preference is made available 86 to apps through a Google Play services API. 87</p> 88<p> 89 Apps making use of the advertising ID <strong>must check for and respect</strong> the 90 user's ad tracking preference. Also please note that any use of the advertising ID 91 must abide by the terms of the <a class="external-link" 92 href="http://play.google.com/about/developer-content-policy.html#ADID">Google Play 93 Developer Content Policies</a>. 94</p> 95 96<h3 id="format">Format of the Advertising ID</h3> 97 98<p> 99 Google Play services APIs expose the user's advertising ID as a string format of UUID, 100 with values similar to this: 101</p> 102<p style="margin-left:1.5em;"><code>“38400000-8cf0-11bd-b23e-10b96e40000d”</code></p> 103 104<h3 id="requirements">Requirements</h3> 105 106<ul> 107 <li>The advertising ID APIs are supported in Google Play services 4.0+</li> 108 <li>Support for the advertising ID on specific devices is based on their installed versions 109 of Google Play services</li> 110</ul> 111 112<h3 id="obtaining">Obtaining the user's advertising ID and ad tracking preference</h3> 113 114<p> 115 If you want to use the advertising ID in your app, you must first install the Google 116 Play services SDK. As noted in the requirements above, you should install the 117 SDK for Google Play services 4.0 or higher if you will develop using the advertising ID 118 APIs. For information about how to get started, see <a href= 119 "{@docRoot}google/play-services/setup.html">Set Up Google Play services</a>. 120</p> 121<p> 122 The advertising ID APIs are available in the <a href= 123 "{@docRoot}reference/com/google/android/gms/ads/identifier/package-summary.html"> 124 <code>com.google.android.gms.ads.identifier</code></a> package in the Google 125 Play Services library. To obtain the user's advertising ID and tracking preference, 126 call the method 127 <a href= 128 "{@docRoot}reference/com/google/android/gms/ads/identifier/AdvertisingIdClient.html#getAdvertisingIdInfo(android.content.Context)"> 129 <code>getAdvertisingIdInfo()</code></a>, which returns an <a href= 130 "{@docRoot}reference/com/google/android/gms/ads/identifier/AdvertisingIdClient.Info.html"> 131 <code>AdvertisingIdClient.Info</code></a> encapsulating the user's current Advertising ID 132 and tracking preference. 133</p> 134 135<p class="note"> 136 <strong>Note:</strong> The <code>getAdvertisingIdInfo()</code> method is a 137 blocking call, so you must not call it on the main (UI) thread. If called on 138 the main thread, the method throws <code>IllegalStateException</code>. 139</p> 140 141<p> 142 Once you've retrieved the <code>AdvertisingIdClient.Info</code> object, you 143 can use it's <a href= 144 "{@docRoot}reference/com/google/android/gms/ads/identifier/AdvertisingIdClient.Info.html#getId()"> 145 <code>getId()</code></a> and <a href= 146 "{@docRoot}reference/com/google/android/gms/ads/identifier/AdvertisingIdClient.Info.html#isLimitAdTrackingEnabled()"> 147 <code>isLimitAdTrackingEnabled()</code></a> methods to access the advertising ID and 148 ad tracking preference. 149</p> 150 151<table> 152<tr> 153<th>Method</th> 154<th>Description</th> 155</tr> 156<tr> 157<td><code>public String getId()</code></td> 158<td style="white-space:nowrap;">Retrieves the advertising ID.</td> 159</tr> 160<tr> 161<td style="white-space:nowrap;"><code>public boolean isLimitAdTrackingEnabled()</code></td> 162<td>Retrieves whether the user has limit ad tracking enabled or not.</td> 163</tr> 164</table> 165 166<p> 167 The advertising ID APIs do not include a "reset" method. Only users can initiate a 168 reset of their own advertising IDs, through the Google Settings application. 169</p> 170<p> 171 For more information about the advertising ID APIs, see the <a href= 172 "{@docRoot}reference/com/google/android/gms/ads/identifier/package-summary.html"> 173 reference documentation</a>. 174</p> 175 176<h3 id="example">Example implementation</h3> 177 178<p> 179 Here's a basic illustration of how you can retrieve the user's advertising ID and ad 180 tracking preference in your app: 181</p> 182 183<pre> 184import com.google.android.gms.ads.identifier.AdvertisingIdClient; 185import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info; 186import com.google.android.gms.common.GooglePlayServicesAvailabilityException; 187import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 188import java.io.IOException; 189... 190 191// Do not call this function from the main thread. Otherwise, 192// an IllegalStateException will be thrown. 193public void getIdThread() { 194 195 Info adInfo = null; 196 try { 197 adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext); 198 199 } catch (IOException e) { 200 // Unrecoverable error connecting to Google Play services (e.g., 201 // the old version of the service doesn't support getting AdvertisingId). 202 203 } catch (GooglePlayServicesAvailabilityException e) { 204 // Encountered a recoverable error connecting to Google Play services. 205 206 } catch (GooglePlayServicesNotAvailableException e) { 207 // Google Play services is not available entirely. 208 } 209 final String id = adInfo.getId(); 210 final boolean isLAT = adInfo.isLimitAdTrackingEnabled(); 211}</pre> 212