#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Google analytics custom tracking


    Hi guys,

    I have a form on a site that pretty much pops up when the user clicks on an link with event.preventDefault(). In other words, the page doesn't reload.

    Now I thought It would be nice to track this with google analytics. If I read the documentation well it offers two methods I think.. (please correct me if I am wrong)

    #1
    Code:
    <a href="#" onClick="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);">Play</a>
    Than the documentation states:
    In this scenario, the reports for Events would display Videos as the Category, Play as the Action, and Baby's First Birthday as the Label. The rest of this document describes these components in detail. Bear in mind that when you implement Event Tracking, you can use this data model as a guide, or you can simply use the _trackEvent() method to segment user interaction in any way that works for your data.
    So clearly that introduces a second method namely: #2 _trackEvent()

    Now the question:
    If I were to use the second method, would I need to declare the _gaq.push still at the top of the page? or can i just do this
    javascript Code:
     
    <script type="text/javascript">
        $(document).ready(function(){
      $('#someElement').click(function(){
     
              _trackEvent('Some_wicked_category', action, opt_label, opt_value, opt_noninteraction)
     
    });
     
        });
     
    </script>

    So in other words, only use the function call without declaring the _gaq variable globally like so:
    javascript Code:
     
    <!DOCTYPE html>
    <html>
        <head><meta charset="UTF-8" />  
            <title></title>
    <script type="text/javascript">
                /* Standard google stuff here */
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-XXXXX-X']);
                _gaq.push(['_trackPageview']);
                // Should I add my custom _gaq.push here??
     
                (function() {
                  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();
            </script>        
        </head>
        <body>
            <div id="someElement">
                bla bla bla
            </div>        
     
            <script type="text/javascript">
                /* ### custom tracking here ### */
                $(document).ready(function(){
     
                    $('#someElement').click(function(){
     
                        //use the tracking event
                        _trackEvent("category", "action");
                        /*
                         * category (required); The name you supply for the group of objects you want to track.
                         * action (required)A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object. 
                         */
     
                    });
     
                });
            </script>
        </body>
    </html>
    Last edited by aeternus; February 17th, 2013 at 06:21 AM. Reason: spelling
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    For anyone interested.
    After a full day google gave back some results. conclusion
    this is wrong:
    Code:
    _trackEvent('form', 'clicked);
    this is right
    Code:
    _gaq.push(['_trackEvent', 'form', 'clicked']);

IMN logo majestic logo threadwatch logo seochat tools logo