In the app build.gradle, remove all dependencies on firebase-core
. Then, add or update the dependency on firebase-analytics
version 17.2.0+. The example below uses a newer version of the SDK.
dependencies {
...
implementation 'com.google.firebase:firebase-analytics:21.0.0'
...
}
2. Enable the feature in your app
Modify your application manifest by adding the following metadata tag to your application tag.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myawesome.app">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MainActivity">
<!-- Value to be added to enable deferred deep links -->
<meta-data android:name="google_analytics_deferred_deep_link_enabled" android:value="true"/>
<activity
android:name=".MainActivity" >
</activity>
</application>
</manifest>
When enabled, GA4F will fetch the configured deep link, on app start, for the corresponding campaign you configured.
Note: Registering a receiver through the manifest comes with drawbacks such as memory usage and startup latency. These may be mitigated by favoring context registry.
3. Capture the deep link
Set up a SharedPreferences change listener within your main launch activity class. This will be triggered when a DDL is available. If you’re registering the listener later in the app lifecycle, the deep link might have already been retrieved. In that case, the listener won’t be triggered, and you can immediately look up the value of the deep link by reading SharedPreferences.
GA4F stores the deep link in a SharedPreferences file google.analytics.deferred.deeplink.prefs
with the key deeplink
. GA4F also stores, in the same SharedPreferences file, the ad click timestamp with the key timestamp
. Note that the format of the timestamp is microseconds (milliseconds followed by a dot and microseconds) and is stored in SharedPreferences using a Long when the type is Double. Use Double.longBitsToDouble(...) to parse the actual value.
Example:
/**
* The main launch activity of the app.
*/
public class MainActivity extends AppCompatActivity {
private SharedPreferences preferences;
private SharedPreferences.OnSharedPreferenceChangeListener deepLinkListener;
@Override
protected void onStart() {
super.onStart();
preferences.registerOnSharedPreferenceChangeListener(deepLinkListener);
}
@Override
protected void onStop() {
super.onStop();
preferences.unregisterOnSharedPreferenceChangeListener(deepLinkListener);
deepLinkListener = null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences =
getSharedPreferences("google.analytics.deferred.deeplink.prefs", MODE_PRIVATE);
deepLinkListener = (sharedPreferences, key) -> {
Log.d("DEEPLINK_LISTENER", "Deep link changed");
if ("deeplink".equals(key)) {
String deeplink = sharedPreferences.getString(key, null);
Double cTime = Double.longBitsToDouble(sharedPreferences.getLong("timestamp", 0));
Log.d("DEEPLINK_LISTENER", "Deep link retrieved: " + deeplink);
showDeepLinkResult(deeplink);
}
};
}
public void showDeepLinkResult(String result) {
String toastText = result;
if (toastText == null) {
toastText = "The deep link retrieval failed";
} else if (toastText.isEmpty()) {
toastText = "Deep link empty";
}
Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_LONG).show();
Log.d("DEEPLINK", toastText);
}
}
4. Prepare data for diagnostic testing
To validate your implementation, get the AdID for the device you wish to test with. You can use the following command to set the DDL that the app will receive.
curl "www.googleadservices.com/pagead/conversion/app/deeplink?&rdid=<<your device adid>>&id_type=adid&bundleid=<<your application package>>&deeplink=<<deeplink you want to receive>>&ddl_test=1"
To check if the deep link was set correctly, you can use this request to verify the response.
curl "www.googleadservices.com/pagead/conversion/app/deeplink?&rdid=<<your device adid>>&id_type=adid&bundleid=<<your application package>>&ddl_test=1"
This test deep link expires after 24 hours. Repeat this step if the test deep link expires.
5. Enable test mode for fetching the test DDL
Enable the DDL test mode to start testing on your devices.
adb shell setprop debug.deferred.deeplink <<your application package>>
Next, enable debug mode on your device. Begin using your app and, in Logcat, check that the log message shows a gmp_version
of at least 18200
. Searching for the keyword deferred
will filter all debug messages from Google Analytics for Firebase relating to that functionality.
Example:
D/FA: Deferred Deep Link feature enabled.
FA-SVC: Uploading data. app, uncompressed size, data: <<your application package>>,
…
gmp_version: 18200