Skip to content

Instantly share code, notes, and snippets.

Track Searches with 0 Results in Universal Analytics

The code would look something like this (you will have to modify this so it will work on your site):

var searchQuery = $('input[class="rsearch_sok2"]').val();
var p = document.location.pathname;
var s = "/?q=" +  searchQuery + "&cat=no-results";
ga('send', 'pageview', {'page':p + s});
/* Track 0 Serch Results with if statment */
var content = document.getElementById('middlewrapper');
if (content.innerHTML.search('Inga produkter hittades!')) {
var sn = "q";
var sr = new RegExp(sn+"=[^\&]+"),
p = document.location.pathname,
s = document.location.search,
sm = s.match(sr).toString(),
@simondahla
simondahla / vwo-blink-prevention.css
Created October 22, 2013 11:49
How to prevent "the blink" in Visual Website Optimizer
@simondahla
simondahla / google-analytics-siteSpeedSampleRate.js
Last active August 4, 2018 17:45
Set proper Sample Rate for Site Speed http://blog.mythly.com/improve-site-speed-in-google-analytics/ Note: Google will automatically cap the sample rate to 10k pageviews a day.
_gaq.push(['_setSiteSpeedSampleRate', 100]);
/*
Google Analytics Classic - Browser Viewport Size
Credit @beantin
CC BY 2.5 2013
*/
function viewport() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
var pixelRatio = (window.devicePixelRatio >= 1.5) ? "high" : "normal";
ga('send', 'pageview', {'dimension5': pixelRatio});
if (typeof jQuery != 'undefined') {
jQuery(document).ready(function($) {
$('form input').blur(function( e ){
var me = $(this), form = me.parents('form');
ga('send', 'event', (me.attr('name') || me.attr('id')), (me.val().length > 0 ? 'filled' : 'empty'), $(this).attr('name')]));
});
}
}
@simondahla
simondahla / advertising.js
Last active January 3, 2016 11:58
Google Analytics Retargeting and Demographics and Interests reports for AdWords and AdBlock software with regular (Google Analytics Classic) fallback.
/* Add this in the top of your advertising script */
_adblock = false;
@simondahla
simondahla / track-downloads-with-universal-google-analytics.js
Created January 16, 2014 17:42
Track, downloads, email, external link clicks with Universal Google Analytics. Include this at the bottom of the desired page. Note: Requiers jQuery to work, include jQuery above this script.
if (typeof jQuery != 'undefined') {
jQuery(document).ready(function($) {
var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
var baseHref = '';
if (jQuery('base').attr('href') != undefined)
baseHref = jQuery('base').attr('href');
jQuery('a').each(function() {
var href = jQuery(this).attr('href');
if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
jQuery(this).click(function() {
@simondahla
simondahla / track-js-error-with-google-analytics.js
Last active October 18, 2018 14:31
A Poor Man's Javascript Error Logger using Google Analytics
window.onerror = function(message, file, line) {
if (typeof(ga) !== "undefined") {
ga('send', 'event', 'Global', 'Exception', file + "(" + line + "): " + message);
}
};