Skip to content

Instantly share code, notes, and snippets.

@smhmic
smhmic / GTM AMP Container Proxy (for sharing a first-party GA client id) .md
Last active July 23, 2020 14:46
GTM AMP Container Proxy (for sharing GA Client ID with non-AMP pages)

This code is for serving an AMP GTM container from your own domain, so that the GA Client ID can be shared between your AMP pages and the rest of your site (enabling them to be tracked together in the same GA property).

Once the endpoint is deployed, you'll need to replace //www.googletagmanager.com/amp.json in the <amp-analytics> embed with the path to your endpoint (leaving the query parameters in place) within your AMP template's source code:

<amp-analytics 
  config="//www.googletagmanager.com/amp.json?id=GTM-XXXXX&gtm.url=SOURCE_URL" 
  data-credentials="include"></amp-analytics>

You can specifiy the cookie domain by appending &amp;cookieDomain=YOURDOMAIN.COM to the endpoint URL.

@smhmic
smhmic / getTLD.js
Last active March 3, 2021 21:13
Get top-level domain (TLD) of current page
function getTLD(){
var cookieName = '_tmp_detect_tld',
globalNamespacePageloadCache = cookieName;
return window[globalNamespacePageloadCache] || ( window[globalNamespacePageloadCache] = (function(){
var i, domain, parts = location.hostname.split('.'),
UNDEFINED = '{{UNDEFINED}}', cKeyValPair, c;
if( window.navigator && navigator.cookieEnabled === false ){ return UNDEFINED+' cookies disabled'; }
for( i=parts.length-1; i>=0; i-- ){
domain = parts.slice(i).join('.');
cKeyValPair = cookieName+'_'+domain+'=1';
@smhmic
smhmic / - Test GA CID persistence in iframe - README.md
Last active October 21, 2020 22:14
Test GA CID persistence in iframe

Test

  1. (optional) Edit code to have custom domain
  2. Paste code into console on any page
  3. Select iframe in console, and paste same code.
  4. Click reload in iframe, and repeat step 3.

Fix

@smhmic
smhmic / UniversalUtmz.js
Created August 8, 2018 03:59
uutmz / UniversalUtmz
;(function(window,undefined){
/*
See LICENSE.txt file included in this distribution.
@version 0.2.0
*/
(function(k){function h(){g.debug&&k.console&&k.console.debug&&k.console.debug.apply(console,["%c[UniversalUtmz]","color:#ccc;border:1px solid #eee"].concat([].slice.call(arguments)))}function r(c,b){var a;if(!c||!b)return!c==!b;for(a in c)if(c.hasOwnProperty(a)&&(!b.hasOwnProperty(a)||c[a]!=b[a]))return!1;for(a in b)if(b.hasOwnProperty(a)&&(!c.hasOwnProperty(a)||c[a]!=b[a]))return!1;return!0}function t(){var c,b={},a,d=/(?:^|[^&]*&)([^=&]+)(?:=([^&]*))?/g;for(c=(c||k.location.search.substr(1)).split("+").join(" ");a=
d.exec(c);)b[decodeURIComponent(a[1])]=a[2]&&decodeURIComponent(a[2]);if(b.gclid)return{medium:"cpc",source:"google",campaign:"{adwords}",keyword:"{adwords}",content:"{adwords}",gclid:b.gclid};if(b.gclsrc)return{medium:"cpc",source:"google",campaign:"{DoubleClick}",keyword:"{DoubleClick}",content:"{DoubleClick}",gclsrc:b.gclsrc};if(b.utm_source)return h("Using data from UTM parameters."),m(
@smhmic
smhmic / ( USAGE ).sh
Created March 21, 2018 19:06
Create new BitBucket repo
# Create an OAuth consumer with write access.
# For a callback URL, you can use: `https://www.google.com/`
# https://bitbucket.org/account/user/USERNAME/oauth-consumers/new
# Optional: Hardcode username, client key, and client secret into script.
# (but be aware of the danger of hardcoding credentials into any code!!)
# (if skipped, the script will securely prompt for info).
# Edit values
NEW_REPO_USER_OR_ORG="editme"
@smhmic
smhmic / GA Management - list recent views (paste in GA UI console).js
Last active November 20, 2017 20:48
GA Management - get list of recent accounts/properties/views (paste in GA UI console)
(function(){
var aI, pI, vI, a, p, v, rows = [], row;
for( aI = 0; aI<window.preload.accounts.length; aI++ ){
row = [];
a = window.preload.accounts[aI];
for( pI = 0; pI<a.wprops.length; pI++ ){
@smhmic
smhmic / - G Apps Script - Open URL .md
Last active February 7, 2025 22:59
Google Apps Script - Open a URL in a new tab (such as directly from clicking a menu item, w/o further user interaction).

Open a URL using Google Apps Script without requiring additional user interaction.

Code also on StackOverflow.

@smhmic
smhmic / Listener - YouTube .gtm.tag.html
Last active June 18, 2024 08:51
Add YouTube JS API support for embedded YouTube videos
<script>(function(){
/*
This script is an alternative to the 'Add JavaScript API support to all YouTube videos'
setting in GTM's YT trigger. GTM's YT trigger will not load the YT API if no videos are
present at page load (it does add the enablejsapi param to dynanmically inserted videos,
but it will not load the YT API js). This script, on the other hand, should work in all cases.
*/
if( window.YT ) return;
@smhmic
smhmic / - README - URL parameter blocklist OR allowlist in GTM.md
Last active May 17, 2022 14:31
URL parameter blocklist/allowlist in GTM.md
@smhmic
smhmic / onDomInsert.js
Created August 3, 2016 20:39
Listener for dynamically inserted DOM elements
/**
* @function onDomInsert - Listener for nodes inserted into DOM.
* Does NOT monitor when things are removed.
* Based on http://stackoverflow.com/a/14570614/445295
* @param [DOMNode] el - Optional. The element to listen on. Defaults to document.
* @param [Function] cb - Callback. Called with inserted element as only arg.
*/
var onDomInsert = (function(){
'use strict';