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

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.


As andreban stated I used:

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

which returns true if it comes from TWA.