Skip to content

Instantly share code, notes, and snippets.

Stephen Harris smhmic

View GitHub Profile
smhmic / GTM AMP Container Proxy (for sharing a first-party GA client id) .md
Last active Jul 23, 2020
GTM AMP Container Proxy (for sharing GA Client ID with non-AMP pages)
View GTM AMP Container Proxy (for sharing a first-party GA client id) .md

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 // in the <amp-analytics> embed with the path to your endpoint (leaving the query parameters in place) within your AMP template's source code:


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

smhmic / getTLD.js
Last active Feb 12, 2020
Get top-level domain (TLD) of current page
View getTLD.js
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';
View - Test GA CID persistence in iframe -


  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.


smhmic / ( USAGE ).sh
Created Mar 21, 2018
Create new BitBucket repo
View ( USAGE ).sh
# Create an OAuth consumer with write access.
# For a callback URL, you can use: ``
# 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
smhmic / GA Management - list recent views (paste in GA UI console).js
Last active Nov 20, 2017
GA Management - get list of recent accounts/properties/views (paste in GA UI console)
View GA Management - list recent views (paste in GA UI console).js
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 / - G Apps Script - Open URL .md
Last active Jul 11, 2020
Google Apps Script - Open a URL in a new tab (such as directly from clicking a menu item, w/o further user interaction).
View - G Apps Script - Open URL .md

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

Code also on StackOverflow.

smhmic / Listener - YouTube .gtm.tag.html
Last active Sep 22, 2017
Add YouTube JS API support for embedded YouTube videos
View Listener - YouTube .gtm.tag.html
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 / gtm-variable-ga-page-custom.js
Last active Jan 8, 2018
Strip URL parameters (GTM Custom JS Variable). Useful for preventing PII from being tracked.
View gtm-variable-ga-page-custom.js
* Returns the current page URL with the specified paramaters stripped.
* Only includes the path + querystring, as this is intended for overriding `page` field of GA Pageview.
* @version 0.1.0
function() {
// The params to strip. Case-insensitive. Escape (with a double-backslash)
// any regex special chars (unless using regex intentionally).
var params = [ 'name', 'email' ];
// You could define the paramaters list as a comma-limited list like this
smhmic / onDomInsert.js
Created Aug 3, 2016
Listener for dynamically inserted DOM elements
View onDomInsert.js
* @function onDomInsert - Listener for nodes inserted into DOM.
* Does NOT monitor when things are removed.
* Based on
* @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';
smhmic / Event-less Data Layer Push Listener.html
Last active May 5, 2016
Lets you use data layer pushes with no 'event' for a GTM Trigger.
View Event-less Data Layer Push Listener.html
* Data layer pushes should always have an event (even in simple <head> snippets).
* But if you're dealing with an event-less push upon which you need to be able
* to fire a Trigger, and you cannot change the push snippet, this code will
* let you detect the push and add an event dynamically.
* Tested in Chrome, Firefox, and IE9.
You can’t perform that action at this time.