Skip to content

Instantly share code, notes, and snippets.

var myElement = document.getElementById('IDoftheDIV');
if(window.addEventListener) {
// Normal browsers
myElement.addEventListener('DOMSubtreeModified', contentChanged, false);
} else
if(window.attachEvent) {
// IE
myElement.attachEvent('DOMSubtreeModified', contentChanged);
}
@simondahla
simondahla / with-analytics.js.html
Last active November 29, 2019 11:24
Updated Instructions for Implementing Google Optimize. Where `GTM-XXXXXX` is the Optimize Container ID & where `GTM-YYYYYY` is the Google Tag Manager Container ID and where `UA-ZZZZZZZZ-Z` is the Google Analytics Property ID. Based on the latest documentation at https://support.google.com/360suite/optimize/answer/7359264
<!DOCTYPE html>
<html>
<head>
<!-- 0. UTF-8 encoding -->
<meta charset="utf-8">
<!-- 1. (OPTIONAL) Move jQuery here. -->
<!-- 2. (OPTIONAL) Datalayer -->
@simondahla
simondahla / optimizely-gtm-custom-integration.js
Last active November 29, 2019 11:23
set d to true for console debugging
(function() {
var d = false;
window.optimizely = window.optimizely || [];
var o = window.optimizely;
if (o.activeExperiments && o.allExperiments) {
for (var i = 0; i < o.activeExperiments.length; i++) {
var experimentId = o.data.state.activeExperiments[i];
var experimentName = experimentId;
$(function() {
var experimentID = optimizely.data.state.activeExperiments[0];
var experimentName = experimentID;
if (typeof optimizely.data.experiments[experimentID] !== 'undefined') {
experimentName = optimizely.data.experiments[experimentID].name;
}
var variationName = optimizely.data.state.variationNamesMap[experimentID];
$(function() {
var experimentID = optimizely.data.state.activeExperiments[0];
var experimentName = experimentID;
if (typeof optimizely.data.experiments[experimentID] !== 'undefined') {
experimentName = optimizely.data.experiments[experimentID].name;
}
var variationName = optimizely.data.state.variationNamesMap[experimentID];
@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 / jquery.link-tracking-ga.js
Last active September 27, 2019 17:03
Google Analytics Link Tracking For both GA Classic & GA UA
// ==UserScript==
// @name Remove loading screen from Google Optimize
// @version 0.1
// @description Script to remove the annoying loading screen...
// @author @simondahla
// @match https://optimize.google.com/optimize/home/
// ==/UserScript==
(function() {
var element = document.getElementsByClassName('opt-busy-dialog')
@simondahla
simondahla / ga-ua-search-tracking-0-results.js
Last active May 2, 2019 18:50
For Universal Google Analytics - Tracking Search Results with 0 hits. Put this in the 0 results page template.
//Define your search query parameter as varible sn
var sn = "q";
var sr = new RegExp(sn+"=[^\&#038;]+");
p = document.location.pathname;
s = document.location.search;
if (!!s.match(sr)) {
sm = s.match(sr).toString();
srs = sm.split("=");
// The next line is where we add the category and add
var pixelRatio = (window.devicePixelRatio >= 1.5) ? "high" : "normal";
ga('send', 'pageview', {'dimension5': pixelRatio});