Code-based Actions: Obtain User page status

You can change the opening event based on a specific page. You can use Javascript to check if the user is on the homepage or another specific page. In this example, we will check if the user is on the homepage then we will automatically open the Web Widget after 5 seconds and trigger a specific event.

To get the code working we will incorporate it by following 3 steps:

  1. Create a function to check if on homepage.
  2. Use the function to check if on homepage.
  3. Add delayed opening & trigger event.

Javascript can also be used to check if the user is on a Desktop or Mobile device or you can Combine Functions to check if the user is on the homepage with a desktop device.

Create a function to check if the homepage

Let's start by creating a function to check if the user is on the homepage. To do that we can use the 'pathname'. If on the homepage the pathname should be equal to "/". Any other page will consist of additional characters in its pathname. If you want to include a specific page you can replace the "/" by the pathname of your page e.g. "/blog".

<script>
  window.homepagecheck = function() {
    var check = false;
    if(document.location.pathname === "/"){
      check=true;
      }
    return check;
  }
</script>

Check if the user is on the homepage

The function is created and we can now call it. The function returns a boolean (true or false). The code below can be translated to something like If it's true the user is on the homepage then ...

<script>
if(window.homepagecheck()){
  // add code here
}

</script>

Add delayed opening and start event

Now let's add the delayed opening and trigger an event.

<script>
  // Delayed Opening
  setTimeout(function() {
   __flowai_webclient_app.open()
  }, 5000) // 5 secs

  // Trigger Event  
  window.\_\_flowai_webclient_autoTriggerEvent = 'START_CHAT'

</script>

Find the complete code below:

<script>
  window.homepagecheck = function() {
    var check = false;
    if(document.location.pathname === "/"){
      check=true;
      }
    return check;
  }


  if(window.homepagecheck()){  
    setTimeout(function() {  
      \_\_flowai_webclient_app.open()  
      }, 5000) // 5 secs

</script>