Search code examples
javascriptmobilebrowser-historyzendesk

Detect if page was opened from app


This applies both to Android and iOS. My web page may be sometimes opened by an app (you go to the app, and click a link there which opens the page). I want to know if the page was accessed through an app or if the user got to it, let's say, by typing the address on the browser. If accessed through an app, I don't need to know which app it was.

The only thing I know of is document.referrer, but it seems to return "" when the page has been opened by the app. Unfortunately using "" as an indicator is not possible, as other ways of getting to the page may also show "" (for example typing the address). The history object does not seem to contain the info I'm looking for either. I am using a Zendesk Help Center, so I only have access to the javascript of the page in order to detect this. I can't make changes on the server-side of my page.

Alternatively, I may be able to talk to the people in charge of the app so that they include something when the app opens the browser which would allow me to access that info on the browser, but I am not sure what that could be. Any ideas?

Thank you!


Solution

  • It seems to me like your best bet would be to have specific links for your site that will let you know that the link came from the app.

    Like so: http://www.yoursite.com/?openedFromApp

    You will use those links inside the app that will be directing users to your website.

    That way, if you were using PHP as your server-side language you'd be able to check if the openedFromApp URL parameter was set like so:

    <?php
    if(isset($_GET['openedFromApp'])) {
        echo "The website was opened by an app";
    }
    else { echo "The website was opened normally"; }
    ?>
    

    If you want to check if the openedFromApp URL parameter is set using Javascript you'd have to create your own function for accessing URL parameters as Javascript does not have a built-in way of accessing them.

    But this link could help you access the URL parameters with Javascript: https://stackoverflow.com/questions/...