Home > Technical & Creative Skills > Programming, Servers & Scripts

Help with Back Button Script (7)


01-31-2017 11:26 AM #1 xkjonz (Member)
Help with Back Button Script

Hi there

Historically, I've been using a separate campaign to track my backbutton conversions. However, I've realized that by doing that, I'm not getting a complete picture of where the backbutton conversions are coming from.

So what I want to do now is to pass my traffic source tokens through my campaign so that whenever a backbutton conversion occurs, I know exactly which placement it came from while at the same time, have the ability to rotate my landers so that when the user hits the backbutton, they will see a different lander.

The script I have been using for my backbutton is:

Code:
<script type="text/javascript">
   window.history.pushState('other.html', 'Other Page', 'other.html');
   window.history.pushState('initial.html', 'Initial Page', 'initial.html');
  </script>

<script type="text/javascript">
   window.addEventListener("popstate", function(e) {
      if(document.URL.indexOf("other.html") >= 0){
      document.location.href = document.location;
      }
   });
</script>
And in both the 'other.html' and 'initial.html' files:

Code:
<html>
<head><meta http-equiv="refresh" content="0;url=http://trackinglink.com?tokens={tokens}" /></head>
<body>
</body>
</html>

TLDR: I need a backbutton script that can rotate landers while at the same time, pass the traffic source's tokens to my tracker.


01-31-2017 11:28 AM #2 erikgyepes (Moderator)

Hello xkjonz,

you can use the following script to do that:

Code:
<script type="text/javascript">function appendqs(t,n){var e=t&&-1!==t.indexOf("?"),o="";return n&&(o=e?"&":"?",t+=o+n),t}var pageInfo={title:document.title,url:location.href},backPageInfo={title:null,url:appendqs("index.html",location.search.substring(1))};window.history.pushState(backPageInfo,null,backPageInfo.url),window.history.pushState(pageInfo,pageInfo.title,pageInfo.url),window.addEventListener("popstate",function(){document.URL.indexOf("index.html")>=0&&(document.location.href=document.location)});</script>
The "index.html" in the code says where the back button should point - in this case "it goes back to itself", but you can redirect to another page for example.


01-31-2017 12:40 PM #3 xkjonz (Member)

Quote Originally Posted by erikgyepes View Post
Hello xkjonz,

you can use the following script to do that:

Code:
<script type="text/javascript">function appendqs(t,n){var e=t&&-1!==t.indexOf("?"),o="";return n&&(o=e?"&":"?",t+=o+n),t}var pageInfo={title:document.title,url:location.href},backPageInfo={title:null,url:appendqs("index.html",location.search.substring(1))};window.history.pushState(backPageInfo,null,backPageInfo.url),window.history.pushState(pageInfo,pageInfo.title,pageInfo.url),window.addEventListener("popstate",function(){document.URL.indexOf("index.html")>=0&&(document.location.href=document.location)});</script>
The "index.html" in the code says where the back button should point - in this case "it goes back to itself", but you can redirect to another page for example.
Thanks @erikgyepes.

Just a clarify - if I wanted to redirect to another lander, would my second lander have to be in same folder as my first lander? Or can I just put in my campaign link so that I can easily change my backbutton lander whenever I want to?


01-31-2017 01:10 PM #4 erikgyepes (Moderator)

I think it could be in other folder as well, but I always included it in the same folder.


01-31-2017 01:36 PM #5 xkjonz (Member)

Awesome! Thanks for the reply. I'll give it a try and see how it goes.


01-31-2017 01:48 PM #6 f3rnok (Member)

Yo,

To rotate your redirect lander you can use something along the lines of this:

Code:
<script>

  // Push a bunch of fake state
  window.history.pushState('http://...', null, null);
  window.history.pushState('http://...', null, null);
 
  var potentialLanders = [
    "http://...",
    "http://...",
    "http://...",
    "http://..."
  ];

  var selectedLander = potentialLanders[Math.floor(Math.random() * potentialLanders.length)];

  window.onpopstate = function() {
    window.location.href = selectedLander;
  };
</script>
You can reference landers on the same server by using relative paths (relative to server root). All the above does is select a lander URL randomly from the list. Ofc you should obfuscate this in some way, so people don't rip your landers en masse


02-01-2017 02:59 AM #7 xkjonz (Member)

Nice tip! I'll give that one a try too


Home > Technical & Creative Skills > Programming, Servers & Scripts