skip to Main Content

I want to track the URL of the current page, then pass it to a URL variable to the next page.

For example 
https://www.mywebsite.com
https://www.mywebsite.com/about/?fromsource=https://www.mywebsite.com
https://www.mywebsite.com/about/careers/?fromsource=https://www.mywebsite.com/about
https://www.mywebsite.com/contact/?fromsource=https://www.mywebsite.com/about/careers

So everytime someone moves to the next page, the URL of the previous page is add to a URL variable in the next page.

2

Answers


  1. I’m not sure if this is the right way. I would use Google Analytics API referral data. But you can create the following shortcode:

    function get_current_page_link () {
        global $wp; //get global wp query
        return home_url( $wp->request ); //get the url of the current page
    }
    add_shortcode('get_current_page_link ', 'get_current_page_link ');
    

    If you want to add the code via PHP code:

    <a href="https://www.mywebsite.com/about/?fromsource=<?php echo do_shortcode("[get_current_page_link]"); ?>">About</a>
    

    If you want to use it in your editor you can add url’s as such:

    https://www.mywebsite.com/about/?fromsource=[get_current_page_link]
    

    The editor in the text mode, it will show up like this:

    <a href="https://www.mywebsite.com/about/?fromsource=[get_current_page_link]">About</a>
    
    Login or Signup to reply.
  2. Why not use document.referrer?
    So e.g. create a new variable in your web-analytics system and use the following js code to populate it:

    function includeReferrer() {
      var ref = document.referrer,
          loc = document.location.href,
          newLoc;
      if (loc.includes('?') != -1) {
        newLoc = loc + "&fromsource=" + ref;
      } else {
        newLoc = loc + "?fromsource=" + ref; 
      }
    }
    

    In this case, you are not overwriting the actual URL, but instead create a new string which includes the requested parameter and can be used for any kind of tracking purpose.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search