Skip to content

Instantly share code, notes, and snippets.

@smhmic
smhmic / console.js
Created February 27, 2024 00:00
Show filtered data layer as table
console.table(dataLayer.filter(v=>v.event=="eventTracking"),['label','action','value'])
@smhmic
smhmic / hyperlink_list.gas.js
Created March 17, 2023 02:43
G Apps Script - hyperlink list / multiple hyperlists in gsheet cell
@smhmic
smhmic / - README - Hubspot refinement CSS & JS .md
Last active September 17, 2021 15:45
Hubspot refinement css/js
<script>
/**
* Deploy clientside to make FB pixels compatible for dual-tracking w/ CAPI.
* Uses https://github.com/smhmic/fbq-spy to listen for data passed to Facebook's
* fbevents.js' `fbq()` and add an event_id to all FB pixels.
*/
window.fbqSpy=window.fbqSpy||function(n){function e(){var e,n=c._fbqOrig=window[u];for(e in r("Hijack",n._fbqOrig?"(already hijacked)":""),window[u]=c,n)n.hasOwnProperty(e)&&(window[u][e]=n[e]===n?c:n[e])}var o,t,a=function(e){if(n=null,e.debugLogPrefix=e.debugLogPrefix||"fbqSpy",e.debug=!!e.debug,!e.callback||"function"!=typeof e.callback){if(e.debug)throw new Error("["+e.debugLogPrefix+"] Aborting; No listener callback provided.");e.callback=function(){}}return e.fbqObjName=e.fbqObjName||"fbq",e}("function"==typeof n?{callback:n}:n),u=a.fbqObjName,i=window[u],r=window.console&&a.debug?function(){var e=[].slice.call(arguments);e.unshift("["+a.debugLogPrefix+"]"),console.log.apply(console,e)}:function(){},l=function(o){var e,n,t={args:o,the:{}},u=t.the;"object"==typeof o[o
@smhmic
smhmic / - README - Add event_id to FB pixels (for redundant deduplicated CAPI tracking).md
Last active May 4, 2023 16:00
Add event_id to FB pixels (for redundant deduplicated CAPI tracking)

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.

Usage:

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
smhmic / get_set_cookie.js
Created February 2, 2021 16:44
Generic Get/Set Cookie Functions
@smhmic
smhmic / - README.md
Last active November 25, 2020 15:47
Track Web Vitals via GTM

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 October 22, 2020 00:26 — 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.
// 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.php
Created July 23, 2020 14:41
GTM AMP Container Proxy (for sharing GA Client ID with non-AMP pages) - for uti.edu
<?php
// Callback for the GET request
function gtmAmpProxy_serve(){
//error_reporting(0);
// Get the hostname of the request origin, and parse it for the pure domain name.
$domain = explode( ':', $_SERVER['HTTP_HOST'] )[0];
if( !( $cookieDomain = @$_GET['cookieDomain'] ) ){
@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.