Skip to content

Instantly share code, notes, and snippets.


Stephen Harris smhmic

  • Philadelphia, PA
View GitHub Profile
View - README - Hubspot refinement CSS & JS .md
smhmic / - README - Add event_id to FB pixels (for redundant deduplicated CAPI tracking).md
Last active Aug 26, 2021
Add event_id to FB pixels (for redundant deduplicated CAPI tracking)
View - README - Add event_id to FB pixels (for redundant deduplicated CAPI tracking).md

This does not cover all aspects of setup; just the web pixel side. All this does is add an event_id to all FB pixels (arguably the most complex part of the clientside setup) without having to modify any pixels directly.

This code is for GTM, but would be trivial to modify code for use anywhere.


In GTM web container ...

  1. Add Facebook Pixel CAPI Helper as a tag; fire on All Pages.
  2. Add Facebook Event ID as a variable.
  3. In GA4 config tag, set event_id = {{Facebook Event ID}}.
smhmic / get_set_cookie.js
Created Feb 2, 2021
Generic Get/Set Cookie Functions
View get_set_cookie.js
smhmic / -
Last active Nov 25, 2020
Track Web Vitals via GTM
View -

This is a pared-down version of Google's web-vitals library for tracking core web vitals RUM via GTM, with various changes.

Google recommends loading it's web-vitals library via a CDN, which adversely impacts performance -- the very thing we're trying to measure & optimize! Using a common CDN has certain benefits mitigating the performance impact, but if we're already loading GTM it would be ideal to instead add the library as a GTM tag. (Actually the ideal way is to package this library with first-party code, but the purpose of Web Vitals trackings aligns nicely with the core needs of teams using GTM.) We lose the advantage of automatic library updates, but we want to make some tweaks anyway...

Primary changes:

  • Only include what we need. This script is ~1/3 smaller than the UMD base+polyfill version of G's library. This script focuses on the three core web vitals,
smhmic / allowlinker-ga.js
Last active Oct 22, 2020 — forked from sahava/allowlinker-ga.js
This script reproduces Google Analytics' (analytics.js) allowLinker/autolinker logic, resulting in a function that returns the GA client id, but only if the autolinker parameter is valid. 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
// Mirrors analytics.js logic to validate autolinker param, and returns cid if valid.
// Returns false if autolinker param found, but invalid.
// Otherwise returns undefined.
var getCidFromAutolinker = function(str) {
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];
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 &amp;cookieDomain=YOURDOMAIN.COM to the endpoint URL.

smhmic / getTLD.js
Last active Mar 3, 2021
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++ ){