ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help with using EE as an URL shortener (tiny/short URLs) considering SEO and google analytics (GA) best practices for tracking

April 24, 2012 10:27am

Subscribe [3]
  • #1 / Apr 24, 2012 10:27am

    DCE Developer

    40 posts

    We’ve implemented a EE installation as a URL shortener. For the most part it is working. This is a corporate shortener. That means there is only a few people that can create the short url’s and we also use it as an automated service for every page.

    The question that I have is regarding SEO practices and Google Analytics best practices. It seems that we are not really getting any tracking of our short URLs. since we use those as a campaign or marketing efforts, this is very important to us. Any insights or help will be appreciated. Attached below is the code we are using.

    NOTE: we thought that maybe immediately embedding the GA code in an embedded template would help us, but it does not look like it. {embed="shorturl/analyticstracking"}

    {embed="shorturl/analyticstracking"}
     {if segment_1==''}
     <?php
     header( "HTTP/1.1 301 Moved Permanently" );
     header( "Location: <a href="http://www.mywebsite.com">http://www.mywebsite.com"</a> );
     exit();
     ?>
     {/if}
    {if segment_1!=''}
     {if segment_2==''}
     {exp:channel:entries channel="shorturl" limit="1" url_title="{segment_1}"}
      {if no_results}
      {redirect="shorturl/404"}
      {/if}
     <?php
     $destinationURL = '{url_destination}';
     header( "HTTP/1.1 301 Moved Permanently" );
     header( "Location: {url_destination}?utm_source=redirect_shorturlsite&utm_medium=link&utm_content=alias&utm;_campaign={url_title}" );
     exit();
     ?>
     {/exp:channel:entries}
     {if:elseif segment_2!='' OR segment_3!='' OR segment_4!='' OR segment_5!='' OR segment_6!=''}
     {redirect="shorturl/404"}
     {/if}
    {/if}
  • #2 / Apr 27, 2012 11:38am

    Man With A Peg

    124 posts

    Hey DCE,

    You are misunderstanding header(‘Location: ‘). This PHP function sends an HTTP header, which the browser *should* receive and react to before it loads anything from the HTML data that follows.

    I am glad that you are calling exit(), since it is good for security and provides a slight code speedup, but that will also prevent the rest of your HTML from going out. Anyway, this is a smaller issue and I’m just explaining what is taking place.

    You could do what Forbes does and place a banner advertisement on a the shortened URL page, then redirect to the correct URL after 5 seconds. This would allow your analytics code to operate.

    HOWEVER, since you are building the shortened URLs yourself, might I suggest that you simply store hits within the database and build your own metrics? It probably won’t be as integrated as what Google provides, but this option would provide a much better experience for your clients.

    UPDATE my_tracking_table SET count = count + 1 WHERE shortcode = "<?php echo addslashes($this->EE->uri->segment(1)); ?>"

    -or-

    INSERT INTO my_tracking_table ('ip', 'shortcode') VALUES ('<?php echo $this->EE->input->ip_address(); ?>', '<?php echo addslashes($this->EE->uri->segment(1)); ?>')
  • #3 / Apr 28, 2012 9:07am

    aelvan

    33 posts

    As EpicVoyage points out, the header(‘Location:...’) function will redirect the browser before any clientside tracking can occur. I don’t think the “wait 5 seconds” is a good solution, it kinda punishes the user for you using a short url. Also, I see the point of getting all your tracking in GA, instead of having local tracking for the short urls. What I’d suggest is doing the GA tracking serverside with php. Have a look at this library for instance: http://code.google.com/p/php-ga/

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases