Forum Discussion

iftomkins's avatar
11 years ago

How do I attach Google Event tracking to a click on the auto-suggest article links

We are using a modified version of the Search Component on our homepage (pretty much exactly like the new search component on the Lithosphere here). 

 

I'd like to trigger a Google Analytics custom event on click of one of the search results (see attached screenshot). For example, I'd like this to run when a search result link is clicked:

 

<script>

 _gaq.push(['_trackEvent', 'SearchAskComponent', 'HomePage', 'clickThrough']);

</script>

 

I've been unable to do this, as described in my previous post here. I would guess Lithium is tracking these click's on their own Lithosphere component, so it would be great if someone could help us track those clicks on our own site, too.

 

 YuriK , DougS  mentioned you're good with GA? Your ideas would be much appreciated!

 

- Alan

 


2014-03-24_1404.png

3 Replies

  • YuriK's avatar
    YuriK
    Khoros Expert
    11 years ago
    Hey Alan,

    Can you please let me know where you put the google analytics java-script that is used to set up the google analytics object? Also, what is the error you are getting when the script runs? Is there a javascript error or something else?

    Please post the code (excluding any tracking codes of course) and let me know where you place it.

    In your previous post, you said that you are seeing some data in your google analytics, is the data coming from the community or from somewhere else?

    Thank you,

    Yuri
  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    Hi Yuri,

     

    Sure thing! Here are the scripts that include GA events. The first 2 work, and the last one which is trying to attach a listener to the auto-suggest results (see screenshot attached) does not work.

     

    As AndrewF mentioned in this previous post, it's likely because the GA event is not bubbling up because of the Lithium script acting on those same auto-suggest results.

     

    - These scripts are in the Hitbox area of the Desktop Skin

    - No javascript errors are occurring, it's just that the script is not running. When a console.log is placed on the 3rd script, its output never appears in the browser console.

     

    1. 

    //Tracks first focus event via Google Analytics. WORKS, currently collecting data.
    $("#custom-search-border .lia-search-input-message").one("focus",function(){
    var category = $(".custom-search-wrapper .search-granularity option:selected").val().split('|')[0];
    _gaq.push(['_trackEvent', 'SearchAskComponent', category, 'focus']);
    console.log('SearchAskComponent: Focus:',category);
    });

     

    2. 

    //change search functionality to Ask a Question post functionality, and track clicks.  WORKS, currently collecting data.
    $("#lia-body .lia-content .custom-search-wrapper .lia-button-Continue-action").click(function() {
    var subject = $(".custom-search-wrapper .SearchForm .lia-search-input-message").val();
    var category = $(".custom-search-wrapper .search-granularity option:selected").val().split('|')[0];
    var url = getPostUrl(category, htmlEncode(subject));
    if (subject != "${text.format('form.AskAnExpert.default_text.title')}") {
    _gaq.push(['_trackEvent', 'SearchAskComponent', category, 'postByClick']);
    window.location.href = url;
    } else {
    }
    });

     

    3. 

    //Trying to track click-throughs of auto-suggest results via GA. Attaching listener to link wasn't working, so this one tries to listen for clicks on the autocomplete container, but this didn't work either. Not working, no data is being collected. Likely because a Lithium script is redirecting you to a new page before this event trigger is able to fire.

    //$("#lia-body .lia-content .custom-search-wrapper .SearchForm.lia-form-inline .lia-autocomplete-container").on("click",function(){
    var category = $(".custom-search-wrapper .search-granularity option:selected").val().split('|')[0];
     _gaq.push(['_trackEvent', 'SearchAskComponent', category, 'clickThrough']);
    });

     


    2014-03-24_1404.png
  • YuriK's avatar
    YuriK
    Khoros Expert
    11 years ago

    Hey iftomkins,

     

    Sorry for not responding to you sooner.

     

    The problem here is that the results don't exist until you start typing items in. So when your javascript is running (on page load) it can't set up the on click on something that doesn't exist.

     

    You may be able to listen to the onkeypress event of the search text field, and then loop through all the result links and attach to their onclick events. I'm not sure if this would work, or if it would cause any performance degradation, but it's worth a try.

     

    Hope this helps,

     

    Yuri