Skip to content

Instantly share code, notes, and snippets.

@thyngster
thyngster / getLoadedGA4Containers.js
Last active May 2, 2024 12:39
Small function to check if any GA4 Container has been currently been loaded.
// DV - 2022 - ES5
// Check if GA4 has been loaded
var getLoadedGA4Containers = function() {
return Object.keys(
window.google_tag_data &&
window.google_tag_data.tidr &&
window.google_tag_data.tidr.container || {}
).filter(function(e){ return e.startsWith('G-') })
}
@thyngster
thyngster / privacy_sandbox_apis_check.js
Created February 27, 2024 21:18
Privacy Sandbox Relevance & Measurement APIs Check
// ----------------------------------------------------------------------
// Privacy Sandbox Relevance & Measurement APIs Check
// ----------------------------------------------------------------------
const privacySandboxApisAvailability = {
topics: 'browsingTopics'in document ? true : false,
attributionReporting: document.featurePolicy.allowsFeature('attribution-reporting') ? true : false,
protectedAudience: 'runAdAuction'in navigator ? true : false,
fencedFrames: 'HTMLFencedFrameElement'in window ? true : false,
sharedStorage: 'sharedStorage'in window ? true : false,
@thyngster
thyngster / return_optimize_containers.js
Created September 26, 2023 15:56
check_optimize_containers
// Returns a list of current Optimize Containers on a page
alert(`Found Optimize Containers: ${Object.keys(window.google_tag_manager || []).filter(e=> e.match(/^(GTM|OPT)/) && !window.google_tag_manager[e].onHtmlFailure)}`)
@thyngster
thyngster / getGa4LinkerParam.js
Created September 15, 2022 21:42
Grab GA4 "linkerParam"
var getGa4LinkerParam = function() {
try {
if (!window.google_tag_data || !window.google_tag_data.glBridge)
return;
var cookies = {};
('; ' + document.cookie).split('; ').forEach(function(ck) {
var name = ck.split("=")[0];
var value = ck.split("=")[1];
if (name && value && name.match(/^_ga$|^_ga_[A-Z,0-9]/)) {
cookies[name] = value.match(/[A-Z,0-9]\.[0-9]\.(.*)/)[1];
@thyngster
thyngster / GA4_eventTimeRemap.js
Last active September 20, 2021 17:55
Remap Events Event Time Parameter as an Event Parameter
(function() {
/*
*
* ( David Vallejo @thyng )
* MIT License
* Remap _et ( Event Time ) as a customEvent Perameter
*
*/
try {
// Monkey Patch, sendBeacon
@thyngster
thyngster / cloudflare_analytics_gtm_compliant_snippet.js
Created December 3, 2020 21:41
CloudFlare Analytics Snippet for GTM
<!-- Cloudflare Web Analytics -->
<script>
(function(){
var s,
r,
t;
r = false;
s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'https://static.cloudflareinsights.com/beacon.min.js';
@thyngster
thyngster / gtm_share_button.js
Last active October 19, 2020 12:46
Enable Share Preview Button . New GTM Preview
(function() {
document.querySelector('.domain-dialog__start-button button').insertAdjacentHTML('afterend', `<button id="share-preview-link" class="btn btn--create" > Share Preview </button>`);
document.querySelector('#share-preview-link').addEventListener("click", function() {
const dummy = document.createElement('p');
dummy.textContent = window.location.href;
document.body.appendChild(dummy);
const range = document.createRange();
range.setStartBefore(dummy);
range.setEndAfter(dummy);
const selection = window.getSelection();
@thyngster
thyngster / gtm-debug-ui-macro-parser.js
Last active October 28, 2019 13:49
Small Snippet to parse Macros on GTM Debug Interface
// Small Snippet to Parse GTM Macros on GTM Debug Interface
// Idea From Simo's Post: #GTMTips: The Mysterious .macro() Call In Custom HTML Tags
// URL: https://www.simoahava.com/gtm-tips/mysterious-macro-call-custom-html-tags/
// Current Version, does not take care of current variable type, all is replaces as str.
(function() {
const iframe = document.querySelector('iframe[src="about:blank"]');
iframe.contentDocument.querySelectorAll('.CodeMirror-code').forEach(function(e) {
if (e.querySelectorAll('.CodeMirror-line').length > 0) {
let _code = [...e.querySelectorAll('pre.CodeMirror-line')].map(function(e) {
if (window.fetch)
fetch('https://www.google-analytics.com/collect').then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}).then(function(response) {}).catch(function(error) {
window.dataLayer.push({
'event': 'ga-blocked'
});
@thyngster
thyngster / object_to_json
Created January 18, 2018 13:40
Exports object as JSON string keeping undefined values
JSON.stringify(obj, function (k, v) {
return (v === undefined) ? "undefined" : v
}).replace(/\"undefined\"/g,undefined);