Skip to content

Instantly share code, notes, and snippets.

@smhmic
smhmic / get_set_cookie.js
Created Feb 2, 2021
Generic Get/Set Cookie Functions
View get_set_cookie.js
@smhmic
smhmic / - README.md
Last active Nov 25, 2020
Track Web Vitals via GTM
View - README.md

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
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
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 //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 &cookieDomain=YOURDOMAIN.COM to the endpoint URL.

@smhmic
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 - README.md

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 / ( 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: `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 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
(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 Aug 27, 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
smhmic / Listener - YouTube .gtm.tag.html
Last active Sep 21, 2020
Add YouTube JS API support for embedded YouTube videos
View Listener - YouTube .gtm.tag.html
<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;