Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created January 31, 2012 21:36
Show Gist options
  • Save livibetter/1713067 to your computer and use it in GitHub Desktop.
Save livibetter/1713067 to your computer and use it in GitHub Desktop.
Using Google Analytics for Website Issue Reporting
/* Copyright 2012 Yu-Jie Lin
* MIT License */
function GAWR(options) {
var self = this;
// -----
this.report = function(entry) {
// Google Analytics
function _report() {
var _gaq = window._gaq || [];
_gaq.push(['_setAccount', self.model.UA]);
if (!self.model.page_tracked) {
_gaq.push(['_trackPageview']);
self.model.page_tracked = true;
}
_gaq.push(['_trackEvent', 'report', entry.issue, entry.info])
if (!window._gaq)
window._gaq = _gaq;
}
if (window._gaq) {
_report();
}
else {
$.ajaxSetup({cache: true});
$.getScript(('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js', _report);
$.ajaxSetup({cache: false});
}
self.view.report_sent();
}
// -----
this.model = new GAWRModel(options);
this.view = new GAWRView(options.target, this.model, this);
}
function GAWRModel(options) {
var self = this;
// -----
// -----
this.UA = options.UA;
this.report_options = options.report_options;
this.page_tracked = false;
}
function GAWRView(target, model, control) {
var self = this;
// -----
this.create_form = function() {
var $target = $('#' + self.target).empty();
var $div = $('<div/>');
$('<label/>').text('Issue')
.attr('for', 'ga-wr-options')
.appendTo($div);
$div.appendTo($target);
$div = $('<div/>');
var $select = $('<select/>').attr('id', 'ga-wr-options');
$.each(self.model.report_options, function(idx, opt) {
$('<option/>').text(opt.title)
.appendTo($select);
});
$select.appendTo($div);
$div.appendTo($target);
$div = $('<div/>');
$('<label/>').text('Additional information')
.attr('for', 'ga-wr-info')
.appendTo($div);
$div.appendTo($target);
$div = $('<div/>');
$('<input/>').attr('id', 'ga-wr-info')
.attr('placeholder', 'Link or any helpful message')
.appendTo($div);
$div.appendTo($target);
$div = $('<div/>');
$('<button/>').text('Report')
.click(self.report)
.appendTo($div);
$div.appendTo($target);
$div = $('<div/>');
$('<span/>').text('This page URL will be logged automatically when reporting')
.attr('id', 'ga-wr-message')
.appendTo($div);
$div.appendTo($target);
}
this.report = function() {
self.control.report({
issue: $('#ga-wr-options option:selected').text(),
info: $('#ga-wr-info').val()
});
}
this.report_sent = function() {
$('#ga-wr-message').hide()
.text('Thanks for reporting!')
.fadeIn('slow');
}
// -----
this.target = target;
this.model = model;
this.control = control;
this.create_form();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment