CardinalPath / gas

Google Analytics on Steroids. A Google Analytics implementation with more power features.
Other
593 stars 78 forks source link

Tracking Qualaroo survey #36

Closed brodseba closed 10 years ago

brodseba commented 11 years ago

Here a code to track Qualaroo survey into GA. For the moment, this code required jQuery. I may need some time to make this code work without jQuery.

/**
 * GAS - Google Analytics on Steroids
 *
 * Track Qualaroo surveys
 *
 * Copyright 2011, Cardinal Path and Direct Performance
 * Licensed under the GPLv3 license.
 *
 * @author Sébastien Brodeur <brodseba@gmail.com>
 */

/**
 * get the form name for a specific elemet
 *
 * @param {DOMElemet} el Dom Element.
 * @return {String} Form Name or Id.
 */
function getFormName(el) {
    while (el && el.nodeName !== 'HTML') {
        if (el.nodeName === 'FORM') {break; }
        el = el.parentNode;
    }
    if (el.nodeName === 'FORM') {
        return el.name || el.id || 'none';
    }
    return 'none';
}

var _gasTrackQualaroo = function (opts) {
    if (!this._qualarooTracked) {
        this._qualarooTracked = true;
    } else {
        //Oops double tracking detected.
        return;
    }

    // Make sure required attrs are defined or fallback to default
    opts['category'] = opts['category'] || 'Qualaroo-';

    // Fire when the user submit the survey.    
    _kiq.push(['eventHandler', 'submit', function () {
        // Retrieve Qualaroo survey ID.    
        surveyId = $("[src*='s3.amazonaws.com/ki.js']").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) _gas.push(['_trackEvent', opts['category'] + surveyId, question, anwser]);
        }
    }
};

_gas.push(['_addHook', '_gasTrackQualaroo', _gasTrackQualaroo]);
eduardocereto commented 11 years ago

This is nice. Thanks for sharing.

brodseba commented 11 years ago

I started from track form and I forgot to remove the getFormName function.

tomfuertes commented 10 years ago

This is awesome! Thanks for sharing. Not pulling this into core until we get a plugin rewrite system where we can generate custom builds. Closing for now and will revisit then!