Skip to content

Instantly share code, notes, and snippets.

@sahava
sahava / customTaskMonster
Last active Aug 6, 2020
One customTask to rule them all.
View customTaskMonster
var _customTask = function () {
// customTask Builder by Simo Ahava
//
// More information about customTask: https://www.simoahava.com/analytics/customtask-the-guide/
//
// Change the default values for the settings below.
// clientIdIndex: The Custom Dimension index to where you want to send the visitor's Client ID.
// https://bit.ly/2Ms0ZcC
var clientIdIndex = 1;
@sahava
sahava / customtask-builder
Last active Feb 26, 2019
customTask Builder
View customtask-builder
<div id="customTaskWrapper">
<table>
<thead>
<tr>
<th>Click to select which feature(s) to include</th>
</tr>
</thead>
<tbody>
<tr>
<td data-customtask-id='cid' data-customtask-selected='false'>Client ID as a Custom Dimension</td>
@sahava
sahava / dataLayerHistory.js
Last active Jul 13, 2020
JavaScript for persisting dataLayer array and data model composition across pages
View dataLayerHistory.js
(function() {
// Set the timeout for when the dataLayer history should be purged. The default is 30 minutes.
// The timeout needs to be in milliseconds.
var timeout = 30*60*1000;
// Change dataLayerName only if you've defined another named for the dataLayer array in your
// GTM container snippet.
var dataLayerName = 'dataLayer';
// Don't change anything below.
View snowplow-application-config
# Copyright (c) 2013-2018 Snowplow Analytics Ltd. All rights reserved.
#
# This program is licensed to you under the Apache License Version 2.0, and
# you may not use this file except in compliance with the Apache License
# Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
# http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License Version 2.0 is distributed on an "AS
# IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
@sahava
sahava / add-missing-datalayer-event.js
Created Feb 26, 2019
This piece of code adds a default 'event' key-value to each dataLayer.push() that hasn't got one.
View add-missing-datalayer-event.js
(function() {
var oldPush = window.dataLayer.push;
window.dataLayer.push = function() {
var states = [].slice.call(arguments, 0);
states.forEach(function(s) {
if (!s.hasOwnProperty('event')) {
s.event = 'default';
}
});
return oldPush.apply(window.dataLayer, states);
@sahava
sahava / allowlinker-ga.js
Last active Feb 9, 2020
This script reproduces Google Analytics' allowLinker plugin, resulting in a function that returns true if the linker parameter in the URL is valid and false otherwise. Uses the "old" linker plugin format. You can also pass a string as an argument to check if that string is a valid linker parameter.
View allowlinker-ga.js
var checkLinker = function(str) {
// First browser fingerprint method.
// Uses the clientId / gid string, user agent, time, and browser plugin descriptions.
var joiner = function (cidGid, offset) {
var a = new Date,
b = window.navigator,
c = b.plugins || [];
var d = [cidGid, b.userAgent, a.getTimezoneOffset(), a.getYear(), a.getDate(), a.getHours(), a.getMinutes() + offset];
for (var e = 0; e < c.length; ++e) {
@sahava
sahava / Facebook Pixel.tpl
Last active May 23, 2019
Facebook Pixel custom tag template for Google Tag Manager
View Facebook Pixel.tpl
___INFO___
{
"displayName": "Facebook Pixel",
"description": "This is a template prototype for the Facebook Pixel.",
"securityGroups": [],
"id": "cvt_temp_public_id",
"type": "TAG",
"version": 1,
"brand": {
View classlist-string
function() {
// Update this array with the class names
var classes = ['class1', 'class2', 'class3'];
// Fetch elements
var els = document.querySelectorAll('.' + classes.join(',.'))
return [].map.call(els, function(e) {
// Get first matching class name per element
return classes.filter(function(c) {
return e.className.indexOf(c) > -1;
View cross-domain-linker.js
// Call with decorateUrl('https://www.domain.com/url', 'UA-12345-1');
// Outputs the URL string with the Universal Analytics cross-domain linker in place.
// Tracking ID is optional. If you leave it out, the linker will be built
// with the settings of the first tracker found on the page.
function decorateUrl(urlString, trackingId) {
var ga = window[window['GoogleAnalyticsObject']];
if (ga && ga.getAll) {
var trackers = ga.getAll().filter(function(t) {
return trackingId ? t.get('trackingId') === trackingId : true;
});
View LookupTableGenerator.gs
// Parse the IDs from the first four rows of the sheet
function getIds() {
const sheet = SpreadsheetApp.getActiveSheet();
const ids = {
accountId: sheet.getRange('B1').getValue().toString(),
containerId: sheet.getRange('B2').getValue().toString(),
workspaceId: sheet.getRange('B3').getValue().toString(),
variableId: sheet.getRange('B4').getValue().toString()
};
if (ids.workspaceId.toLowerCase() === 'default') ids.workspaceId = getDefaultWorkspaceId(ids.accountId, ids.containerId);
You can’t perform that action at this time.