Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if my website is opened inside a Trusted Web Actvity?

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?

like image 742
andreban Avatar asked Feb 07 '19 18:02

andreban


2 Answers

There are three options that will help detecting if the page is being opened from inside a Trusted Web Activity:

  1. When opening the page, the Referral will be 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>')
  1. 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.

  2. 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.

like image 149
andreban Avatar answered Sep 18 '22 02:09

andreban


As andreban stated I used:

document.referrer.includes('android-app://')

which returns true if it comes from TWA.

like image 28
Gedoba Avatar answered Sep 18 '22 02:09

Gedoba