[UPDATE - January, 19th 2012] I update the code so it is compatible with all question type.
[UPDATE - January, 25th 2012] I fix two bug. 1) Now the GA event is fire. 2) surveyId extraction is cleaner.
[UPDATE - February, 23th 2012] Added some screenshot from Google Analytics
[UPDATE - August, 16th 2012] More complete code
For a while, I searched a way to track KISSInsights with Google Analytics (GA). I wished to use the GA _trackEvent tag to capture the actual question/answer.
Here the code (using jQuery):
For example:
Here a example on how to create a custom report in Google Analytics to categorize outcome base on survey answer.
And here a example directly from the Google Analytics Events report.
Enjoy and feel free to let me know/share if you like :-)
[UPDATE - January, 20th 2012] You could easily modify the tracking to use an alternate version. Your choice.
[UPDATE - January, 25th 2012] I fix two bug. 1) Now the GA event is fire. 2) surveyId extraction is cleaner.
[UPDATE - February, 23th 2012] Added some screenshot from Google Analytics
[UPDATE - August, 16th 2012] More complete code
For a while, I searched a way to track KISSInsights with Google Analytics (GA). I wished to use the GA _trackEvent tag to capture the actual question/answer.
Here the code (using jQuery):
- Load Google Analytics. Make sure you use your UA.
- Load KISSInsights (now Qualaroo). Make sure you use your survey code.
- Check if jQuery is available, if not, load it
- Link Qualaroo with Goolge Analytics
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXX-X']); _gaq.push(['_trackPageview']); (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><script type="text/javascript">var _kiq = _kiq || [];</script><script type="text/javascript" src="//s3.amazonaws.com/ki.js/XXXXX/XXX.js" async="true"></script><script type="text/javascript"> (function() { // if jQuery isn't loaded, do so now and attach an onload event to fire initGA if (typeof jQuery === "undefined") { var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.src = '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; jq.onreadystatechange = function() { if (this.readyState === 'complete') { init(jQuery); } }; jq.onload = init; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jq, s); } else { // jQuery is already loaded, we can use it jQuery(document).ready(function($) { init($); }); } })(); function init($) { // This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. // Fire when the user submit the survey. _kiq.push(['eventHandler', 'submit', function(){ // Retrieve KISSInsights survey ID. surveyId = $("[src*='kissinsights.com']").attr("src").match(new RegExp("(id=)([0-9]*)&"))[2]; // For each survey questions... for (i = 0; i < $("[class*='ki_question']").length; i++) { // ... get the question text... question = $("[class*='ki_h1']:eq("+i+")").text(); // ... and the selected anwser text // Check first for radio button answer. anwser = $("[class*='ki_question']:eq("+i+") input:checked[type='radio']").closest("label").text(); // Then for checkbox answer if (anwser == "" || anwser == null || anwser == undefined) { anwser = $("[class*='ki_question']:eq("+i+") input:checked[type='checkbox']").closest("label").text(); } // Then for textbox answer(s) if (anwser == "" || anwser == null || anwser == undefined) { anwser = $("[class*='ki_question']:eq("+i+") textarea").val(); } // And finaly, score if (anwser == "" || anwser == null || anwser == undefined) { anwser = $("[class*='ki_question']:eq("+i+") [class*='active'] a").text(); } // If both data are available, queue a Google Analytics _trackEvent tag. if (question && anwser) { _gaq.push(['_trackEvent', 'VOC' +' - ' + surveyId, question, anwser]); } } }]);</script>Basically, this code is trigger when a user submit his answer. It will create a _trackEvent tag for each question/answer(s) pair. The beauty of the code is that it fetch the actual text value automatically and save it in a readable way into GA.- Event category: Always VOC (you can change it to any thing you like.)
- Event action: The actual survey id (so if you have multiple survey running at the same time, each one would be save separately in GA.)
- Event label: "Question text | Answer(s) text"
For example:
- Which traffic source yield better satisfaction?
- Does satisfaction impact goal completion rate?
- Does satisfaction impact frequency/recency of visitor?
Here a example on how to create a custom report in Google Analytics to categorize outcome base on survey answer.
And here a example directly from the Google Analytics Events report.
Enjoy and feel free to let me know/share if you like :-)
[UPDATE - January, 20th 2012] You could easily modify the tracking to use an alternate version. Your choice.
if (question && anwser) _gaq.push(['_trackEvent', 'VOC-' + surveyId, question, anwser]);- Event category: VOC-surveyID (so if you have multiple survey running at the same time, each one would be save separately in GA.)
- Event action: Question text
- Event label: Answer(s) text


16 comments:
This is great! I was looking for the same functionality a while back and even contacted kiss metrics about making it easier to latch on the questions.
Can't wait to see some user segmented data :)
Same for me. :-)
I will surely "extend" that post in the next couple of days to include screen capture from GA showing some use of this data.
I update the post. Enjoy!
Just fix two bugs:
1 - Now the GA event will be fire. (sorry for this one.)
2 - The surveyId extraction is cleaner now.
great! try to use it at contact form 7 wordpress plugin... ;)
Hi Sebastien,
Thanks so much for this, I've started using it last week but my problem is it seems to only capture a small number of submissions as events (only 11 when I have had over 300 surveys filled). I'm thinking the order of the scripts is important, if not, do you have any idea what could be causing this? Where does this bit get placed? After GA AND KissInsights script?
Thanks
Yes, it's better to put my code after the GA and KISSInsights codes.
Sébastien,
Thank you very much for making this code public. Can't wait to feed this data into my GA account.
HI,
Thank you! this is cool.
Any idea how long an answer you can keep in the GA event label?
tried installing the code,but doesn't seem to work. the code just displays as is when viewing the site.
am i supposed to wrap script tags around this? don't know coding at all. would be great if you can provide instructions. thanks in advance.
@gymrat: Yes, this code should go in a <script> tag. You could simply put this just after your _gaq.push(['_trackPageview']); and it should work. Good luck
@Ariel: I believe this could be very long, so I won't worry about it.
thank you sebastien, but it did not work for me.
i didn't get any errors, just no tracking data in GA. strange.
am i supposed to add a reference to the jquery file or include "$(document).ready(function() {" in the script?
@gimrat: I update the code in the blog post. You should now have a more complete version of the code. Juste make sure you set you Google Analytics UA number (_setAccount) and make sure you use the KISSInsights/Qualaro script they provide you.
thanks for making it super easy for people like me. but unfortunately, i tried it but still to no avail. :( i double, triple checked but still not getting it to show up in GA.
i give up and will wait till qualaroo integrates this.
thanks very much again for your help nonetheless.
Post a Comment