Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Exit Page - How to


Reply


2 Posts   1398 Views

Avatar
w0lf

Community Member, 1 Post

17 October 2009 at 1:03am

Hi there,

just registered for this forum. I already made 3 sites with SilverStripe and it just works like a charm.

The only thing, that i didn't find out is, how to install/configure a "Exit Page". So I did it on my own ... it works, but I'm sure there is a better way to do this (Creating a real http filter instead of replacing external urls within the content ... ?)

The reason for using a exit page is, to have google analytics (or any other) to track how often which external links are being clicked. Maybe there is a better way to do this anyway?

1. I created a Page called www.somehost.com/Exit and disabled "show in menu"
2. Page.php: I replace all external urls to point to this new page with a GET Parameter 'exiturl'

class Page_Controller extends ContentController {
...
   function isInternalUrl($url)
   {
      $internal_urls = array('somehost.com', 'www.somehost.com', 'sta.www.somehost.com');
      foreach($internal_urls as $intUrl)   {
         if(strpos($url, $intUrl) === false) {
            continue;
         } else {
            return true;
         }
      }
      return false;
   }
...
   public function init() {
      parent::init();
...
      // track all external urls and add exit page
      if(array_key_exists('exiturl',$_GET)) {
         // Add the exiturl to the content
         $this->dataRecord->Content = str_replace('[EXITURL]', $_GET['exiturl'], $this->dataRecord->Content);
      } else {
         // replace external urls with the exit page
         $exitpage = '/exit/?exiturl=';
         $regex = '/https?\:\/\/[^\" ]+/i';
         preg_match_all($regex, $this->dataRecord->Content, $matches);
         foreach($matches[0] as $url) {
            if(!$this->isInternalUrl($url)) {
               $this->dataRecord->Content = str_replace($url, $exitpage . $url, $this->dataRecord->Content);
            }
         }
      }
...
   }
...
}


3. Then I just add "[EXITURL]" within the new page with the editor (if javascript is disabled the user can click on the link, and for SEO purposes) and have a javascript redirect which gets the GET parameter.

<p>Your are leaving www.somehost.com and are being redirected to another page.</p>
<p>If you are not being redirected click <a href="[EXITURL]">here</a>.</p>
   <script type="text/javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}      
      if(location.href.indexOf('exiturl') != -1)
      {
         location.replace(gup('exiturl'));
      }
   </script>


I think you have to add this javascript within the template. The WYSIWYG editor deletes javascripts (as it seems!).

Do you have any ideas if there is an existing solution? Maybe you have a better idea how to realize this?

Thanks in advance

w0lf

Avatar
Greg1

Community Member, 28 Posts

19 October 2009 at 11:25am

Edited: 19/10/2009 11:59am

Hi Wolf

I prefer to track exit links in Google Analytics via onClick events. Here is the google help link: http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55527

You can also do this using trackEvent rather than trackPageview so it doesn't inflate your site pages views but I don't have a link handy for that.

You can get some javascript that does this automatically. [url=http://lmgtfy.com/?q=google+analytics+automatically+track+outbound+links]Google[/url] should return some options and you can also do it with jQuery if you prefer.