Javascript (Google analytics tracking code) in PHP file

Discussion in 'PHP' started by a-ha, Dec 4, 2009.

  1. #1
    I use redirect pages for my affiliate links. So, I have file.php that contains redirect to my affiliate link. I would like to track it with Google Analytics. Should I just place js tracking code before php redirect? Should I strip <script type="text/javascript"> ... </script> Should I place tracking code before or after opening code: <?php

    Here is exapmle of what I want to put together, could someone show me the right way:

    GA js code (new asynchronous tracking code):

    <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
    _gaq.push(['_setDomainName', '.example.com']);
    _gaq.push(['_trackPageview']);

    (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
    })();

    </script>

    and PHP redirect:

    <?php header("Location: http://www.example.net/xxxxxxxxxxxxxxxx"); ?>
     
    a-ha, Dec 4, 2009 IP
  2. w3goodies

    w3goodies Member

    Messages:
    94
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Make your PHP file look like this:

    
    <html>
    <head>
    <script type="text/javascript">
    
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
    _gaq.push(['_setDomainName', '.example.com']);
    _gaq.push(['_trackPageview']);
    
    (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
    })();
    window.location = "http://www.example.net/xxxxxxxxxxxxxxxx";
    </script>
    </head>
    </html>
    
    PHP:
    Hope it helps!
     
    w3goodies, Dec 4, 2009 IP
    a-ha likes this.
  3. a-ha

    a-ha Peon

    Messages:
    120
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your help. I guess it should then be named file.htm.

    But I'd rather go with php redirect instead of js. Because I'll add referrer info from cookie to link, like this:

    <?php header("Location: http://www.example.com/xxxxx?tracking=".$_COOKIE['RefererCookie']); ?>

    I don't know how to do that with js.
     
    a-ha, Dec 4, 2009 IP
  4. w3goodies

    w3goodies Member

    Messages:
    94
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    You can do it simply like this:
    
    
    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
    _gaq.push(['_setDomainName', '.example.com']);
    _gaq.push(['_trackPageview']);
    
    (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
    })();
    
    </script>
    <?php
    header("Location: http://www.example.com/xxxxx?tracking=".$_COOKIE['RefererCookie']);
    ?>
    
    PHP:
     
    w3goodies, Dec 4, 2009 IP
  5. a-ha

    a-ha Peon

    Messages:
    120
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, I tried that before posting this thread.
    I have placed js tracking code before php redirect, named file somename.php and placed it on subdomain - _gaq.push(['_setDomainName', '.example.com']); should make it work across subdomains. Since it isn't tracking I thought that I can't just put js code in php file.

    Maybe I should first try with old GA tracking code.
     
    Last edited: Dec 4, 2009
    a-ha, Dec 4, 2009 IP