Solved

Jquery Type ahead Google Analytics Custom field

Posted on 2014-10-13
7
86 Views
Last Modified: 2016-02-24
Below, I have the following code that is for a type ahead search for FAQ content. It is working properly.  However, when I try to add Google Analytic code where the search term parameter is located it breaks.

if (!window.console) console = {log: function() {}};

$("#jsoninfo").val = '';
    
$(document).ready(function () {
        $().SPServices({
            operation: "GetListItems",
            async: false,
            webURL: "http://itsusmpw00799:56789/prd/hcctesting/faq/",
            listName: "FAQ",
            CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='label' /><FieldRef Name='Title' /><FieldRef Name='Name1' /><FieldRef Name='full' /></ViewFields>",
            CAMLQuery: '',
            completefunc: function (xData, Status) {

var jsondata                
jsondata = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({ 
  mapping: {
     ows_Title: {mappedName: "tags", objectType: "Text"},
     ows_label: {mappedName: "question", objectType: "Text"},
     ows_Name1: {mappedName: "body", objectType: "Text"}},
  includeAllAttrs: false,
  removeOws: true
}); 
                        
//console.log(jsondata);

var jsonstring = JSON.stringify(jsondata); 

                
$('#jsoninfo').val(jsonstring);

var adamjson = $('#jsoninfo').val();
var jsonparse = $.parseJSON(adamjson); 
//console.log(jsonparse);                                          
     }   
            
    });
    });
    
$(function(){
$("#s1").autocomplete({ 
    minLength: 3,
    cache: false,
    datatype: "json",
    source: function (request, response){
        var jsonparse = $.parseJSON($("#jsoninfo").val());
        //console.log(jsonparse);
        var searchParam =request.term.toLowerCase();
        var results = [];
        for (var i = jsonparse.length - 1; i >= 0; i--) {
        var $tags_array = [];    
        var $tags_array = jsonparse[i].tags.split(';');
            
if($.inArray(searchParam,$tags_array)>-1||jsonparse[i].question.toLowerCase().indexOf(searchParam) > -1) {

results.push(jsonparse[i].question);
response(results);
}
                  
    } 
console.log(results);
$("#jsoninfo2").val(JSON.stringify(results));    
    },
      select: function(event, ui) {
        var jsonparse = [];
        var jsonparse = $.parseJSON($('#jsoninfo').val());
        var answer = [];
        for (var i = jsonparse.length - 1; i >= 0; i--) {
        if(jsonparse[i].question.indexOf(ui.item.label) > -1) {
        
        $("#jsoninfo2").val('<div class="question">' + jsonparse[i].question + '</div><br><div class="answer">' +  jsonparse[i].body+'</div>');
            
       
    
     
    }
}
console.log(answer);
        $("#popuppanel").fadeIn('slow');
        $('#faq_label').html('<div class="close" onclick="faqclose()">×</div><br>' + $("#jsoninfo2").val());
        $('#faq_label a').live('click', function() {
        window.open($(this).attr('href'));
        return false;
});
		$("#popuppanel").focus(); 
        $(this).val(''); return false;     
    }
    
}
                     )

$('#s1').attr("value", "");
}
);


function faqclose() {
       $("#popuppanel").fadeOut('slow'); 
       
	  }



   

Open in new window



Where should I embed the GA code?

_gaq.push(['_setCustomVar',
      1,                   // This custom var is set to slot #1.  Required parameter.
      'Search_Term',     // The name acts as a kind of category for the user activity.  Required parameter.
      $("#s1").val,               // This value of the custom variable.  Required parameter.
      2                    // Sets the scope to session-level.  Optional parameter.
   ]);
 _gaq.push(['_trackEvent',
      'Search', // category of activity
      'Item Add', // Action
   ]);      

Open in new window



Is there an easier way to do this?
0
Comment
Question by:aehrenwo
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40377595
line 4 should be : $("#s1").val(),
instead : $("#s1").val,
0
 

Author Comment

by:aehrenwo
ID: 40377661
Thanks for the correction. I am also having trouble knowing where the Google code should be placed in the jquery to send the search term every time it gets entered by the user. Where ever I add it in the type ahead stops working or only shows 1 result.

Please advise.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40377762
inside the complete callback function :
completefunc: function (xData, Status) {
 // here

or :
select: function(event, ui) {
// here once the user select one
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:aehrenwo
ID: 40377795
I tried do it this way... but it doesn't seem to be working still.. sorry but I am just not clear if I need to put the code somewhere separate and just reference it after the other function.

console.log(results);
$("#jsoninfo2").val(JSON.stringify(results));    
    },
      select: function(event, ui) {
        _gaq.push(['_setCustomVar',
      1,                   // This custom var is set to slot #1.  Required parameter.
      'Search_Term',     // The name acts as a kind of category for the user activity.  Required parameter.
      $("#s1").val(),               // This value of the custom variable.  Required parameter.
      2                    // Sets the scope to session-level.  Optional parameter.
   ]);
 _gaq.push(['_trackEvent',
      'Searh_Term', // category of activity
      'Item', // Action
   ])  
            
        var jsonparse = [];
        var jsonparse = $.parseJSON($('#jsoninfo').val());
        var answer = [];
        for (var i = jsonparse.length - 1; i >= 0; i--) {
        if(jsonparse[i].question.indexOf(ui.item.label) > -1) {
        
        $("#jsoninfo2").val('<div class="question">' + jsonparse[i].question + '</div><br><div class="answer">' +  jsonparse[i].body+'</div>');
                   
    
     
    }
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40377838
place an alert in front of this code, do you get it?
if yes, test your push alone (click on a button) to be sure it work alone
0
 

Author Comment

by:aehrenwo
ID: 40377849
Ok I will try that. If I have trouble I will let you know,
0
 

Author Comment

by:aehrenwo
ID: 40392158
There is activity that is showing up in the Realtime area in analytics but the Custom Variables are not appearing in the Audience/Custom section.

search.jpg - I want to have the search term(s) as one custom variable , in the input id "s1". and the search results are in json form in hidden text area with id "jsoninfo2"

Is there key designation that I need to use? Is the code not defined properly? This is the first time I am trying to pass this kind of autocomplete search content to GA.

Help!

thanks,

Adam
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This Micro Tutorial will demonstrate using Google Doc how to import live data to another spreadsheet in Google Spreadsheets using the IMPORTRANGE function.
This Micro Tutorial will demonstrate Google Calendar to monitor updates with top sites, such as Facebook, Google, Twitter, etc. with Marketing News. Each update of Google Calendar can be monitored, correlate dips and spikes in your website traffic, …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now