Passing Information to a Trusted Web Activity using Query Parameters Stay organized with collections Save and categorize content based on your preferences.
When using Trusted Web Activity in their applications, developers may need to pass information fromthe native part of the application into the Progressive Web App (PWA).
A common use-case for this is implementing custom analytics segmentations to measure installationsand sessions started from the Trusted Web Activity. Query parameters can be added to the launch URLto implement this.
Modifying the start URL
If the parameter being passed to the PWA will remain the same across users andlaunches, the parameter can be appended directly to the launch URL. An example of this usage iswhen developers want to measure the number of navigation sessions created from a Trusted WebActivity.
Using Bubblewrap
Bubblewrap is a tool created to help developers to creating a Project for anAndroid application that launches an existing PWAs using a Trusted WebActivity. It contains both alibrary and aCommand Line Interface (CLI).
Creating a new project
When using theBubblewrap CLI, a project is initialized with theinit
command, and createsdefault values from a Web Manifest, provided as a parameter:
bubblewrapinit--manifesthttps://material.money/manifest.json
The wizard will use the start_url from the Web Manifest as default and will ask users to confirmthe value, giving developers the chance to add extra parameters to the url used to start theProgressive Web App.
Modifying an existing project
When Bubblewrap generates a project, information for that particular project is stored in a filecalledtwa-manifest.json
, in the project folder. To modify the start url for existing project,developers need to modify the file:
{..."startUrl":"/?utm_source=trusted-web-activity",...}
Then, re-generate the project files and apply the new start URL
bubblewrapupdate
Using Android Studio
When using Android Studio and the default LauncherActivity, the startUrl is defined as a meta taginside AndroidManifest.xml, and we can change the URL used to launch the Trusted Web Activity bymodifying it:
<activityandroid:name="com.google.androidbrowserhelper.trusted.LauncherActivity"android:label="@string/app_name">...<meta-dataandroid:name="android.support.customtabs.trusted.DEFAULT_URL"android:value="https://svgomg.firebaseapp.com/?utm_source=trusted-web-activity"/>...</activity>
Modifying the start URL dynamically
In other cases, developers may want to create parameters that change across users or sessions, forinstance. In most cases, this will involve collecting details from the Android side of theapplication to pass it to the Progressive Web App.
Step 1: Create a custom LauncherActivity
publicclassCustomQueryStringLauncherActivityextendsLauncherActivity{privateStringgetDynamicParameterValue(){returnString.valueOf((int)(Math.random()*1000));}@OverrideprotectedUrigetLaunchingUrl(){// Get the original launch Url.Uriuri=super.getLaunchingUrl();// Get the value we want to use for the parameter valueStringcustomParameterValue=getDynamicParameterValue();// Append the extra parameter to the launch Urlreturnuri.buildUpon().appendQueryParameter("my_parameter",customParameterValue).build();}}
Step 2: Modify theAndroidManifest.xml
to use the custom LauncherActivity
<activityandroid:name="com.myapp.CustomQueryStringLauncherActivity"android:label="@string/app_name">...<meta-dataandroid:name="android.support.customtabs.trusted.DEFAULT_URL"android:value="https://squoosh.app/?utm_source=trusted-web-activity"/>...</activity>
Conclusion
Passing information from the native part to the web part of an application can be achieved by usingquery parameters. When a parameter is added to the query string, it will be accessible to scriptsrunning on the page and may also be part of the referral when users navigate to a different page orthe developer implements a share action.
Developers must be aware of those implications, and can mitigate them usinglink rel=noreferrer or cleaning-up the URL using thepage location API.
The Trusted Web Activity protocol doesn't currently provide a mechanism to exchange messages withthe native part of the application after the web part is invoked.
We believe existing or upcoming Web Platform APIs enable most use cases needed by developers. Ifyou are looking for new or upcoming Web APIs, check out theNew Capabilities status page.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-01-17 UTC.