none
Google Analytics event tracking using _trackEvent RRS feed

  • Question

  • Hi,

    I'm trying to use Google Analytics to track when use clicks a button within Word/Excel app. GA seems to work fine (record a hit) for the app HTML page, but for some reason the following code doesn't cause hit to be recorded to Google. I'm seeing the HTML page's hit being sent to GA in Fiddler, but nothing when I click a button where GA push is included. Button itself works fine, and other javascript in the event handler is run just fine.

    _gaq.push(['_trackEvent', 'Analyze', 'Submit']);

    _gaq.push is in fact returning 0, indicating it didn't succeed. I've tried moving the initialization script of GA around, but it has no effect. 

    I even made a simple test button on the app, and it's just giving me 0 return value.

    <a href="#" onClick="$('#garetval').html(_gaq.push(['_trackEvent', 'test1', 'test2']));">Test</a><div id="garetval"></div>

    Any tips?

    Thursday, January 24, 2013 2:48 PM

Answers

  • I was including it normally in the App's main .html file. Now moved it to Office.Initialize, and at least the initial App load appears in GA (that was working earlier as well).

    Here's overview of how I'm doing it (taken from the .js file referenced by the app's .html file): 

    var _gaq = _gaq || [];
    
    Office.initialize = function (reason)
    {
        // Google Analytics
        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);
    
        // Checks for the DOM to load.
        $(document).ready(function ()
        {
            // Primary GA hit when app is loaded
            _gaq.push(['_setAccount', 'UA-12123123-2']); 
            _gaq.push(['_trackPageview']); // THIS GETS LOGGED FINE IN GA
            
            $("#analyzeTextbtn").click(function ()
            {
                getAndAnalyzeData();
            });        
        }
    }
    
    function getAndAnalyzeData()
    {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
        function (result)
        {
            if (result.status == "succeeded")
            {            
                SubmitText(result.value);
            }
        });
    }
    
    function SubmitText(txt)
    {
      // Sanity checks
      
      // Push to google if all is good
      _gaq.push(['_trackEvent', 'Analyze', 'Submit']); // THIS WORKS
      
      //Do the rest
    }

    EDIT: Turns out this works fine, but Events tracking just takes 24 hours or so to show in GA.
    • Marked as answer by Jussi_Palo Friday, February 1, 2013 6:41 AM
    • Edited by Jussi_Palo Friday, February 1, 2013 6:43 AM
    Thursday, January 24, 2013 5:21 PM

All replies

  • How are you including the ga.js library?

    Try to inclkude it like this:

    Office.Initialize = 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);
    }


    Anze Javornik

    Thursday, January 24, 2013 3:56 PM
  • I was including it normally in the App's main .html file. Now moved it to Office.Initialize, and at least the initial App load appears in GA (that was working earlier as well).

    Here's overview of how I'm doing it (taken from the .js file referenced by the app's .html file): 

    var _gaq = _gaq || [];
    
    Office.initialize = function (reason)
    {
        // Google Analytics
        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);
    
        // Checks for the DOM to load.
        $(document).ready(function ()
        {
            // Primary GA hit when app is loaded
            _gaq.push(['_setAccount', 'UA-12123123-2']); 
            _gaq.push(['_trackPageview']); // THIS GETS LOGGED FINE IN GA
            
            $("#analyzeTextbtn").click(function ()
            {
                getAndAnalyzeData();
            });        
        }
    }
    
    function getAndAnalyzeData()
    {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
        function (result)
        {
            if (result.status == "succeeded")
            {            
                SubmitText(result.value);
            }
        });
    }
    
    function SubmitText(txt)
    {
      // Sanity checks
      
      // Push to google if all is good
      _gaq.push(['_trackEvent', 'Analyze', 'Submit']); // THIS WORKS
      
      //Do the rest
    }

    EDIT: Turns out this works fine, but Events tracking just takes 24 hours or so to show in GA.
    • Marked as answer by Jussi_Palo Friday, February 1, 2013 6:41 AM
    • Edited by Jussi_Palo Friday, February 1, 2013 6:43 AM
    Thursday, January 24, 2013 5:21 PM
  • What if you try to not have it async?

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._trackPageview();
    ...
    <a onclick="pageTracker._trackEvent('category', 'action', 'opt_label', opt_value);">click me</a>

    Or if you maybe add this line as the first push in the SubmitText function

    _gaq.push(['_setAccount', 'UA-12123123-2']); 


    Anze Javornik

    Thursday, January 24, 2013 5:43 PM
  • As I was suspecting, GA seems to cache the pushes in some way, AND results are not immediately visible in GA. It looks like all test I did yesterday went through even though I didn't see them in Fiddler. 

    I will have to wait a while to see and confirm my current code works, it is now the one I posted above. Will get back to this in a few days.

    Thanks Anze for your help :)


    Friday, January 25, 2013 8:17 AM