I'm opening my page inside a Trusted Web Activity and I want to detect when it's being opened inside it to customize behaviour and for analytics purposes. How can I detect that the page is being opened from the TWA?
There are three options that will help detecting if the page is being opened from inside a Trusted Web Activity:
android-app://<twa.package.name>
, where twa.package.name is the package name used on the Android side of the Trusted Web Activity. In JavaScript, check if the users is in the Trusted Web Activity:document.referrer.includes('android-app://<twa.package.name>')
Adding an URL parameter. Append a query string to the end of the URL that is launched with the PWA. There's more information in the Passing Information into a Trusted Web Activity using Query Parameters article.
Request Headers: you can now set custom request headers when the origin is verified against the app. This article explains how do do it. This approach is not yet supported by all browsers though.
As andreban stated I used:
document.referrer.includes('android-app://')
which returns true if it comes from TWA.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With