Skip to content

Instantly share code, notes, and snippets.

View thyngster's full-sized avatar

David Vallejo thyngster

View GitHub Profile
@thyngster
thyngster / ga4CookieParser.js
Created April 12, 2025 06:01
GA4 COOKIES PARSER
/**
* Parses GA4 cookies using a measurement ID without polluting the global namespace
* @param {string} measurementId - GA4 measurement ID (e.g., 'G-XXXXXXXX')
* @returns {Object|null} Parsed GA4 cookie data or null if cookies not found
*/
function parseGA4Cookies(measurementId) {
function getCookieValue(name) {
const match = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
return match ? match.pop() : null;
}
@thyngster
thyngster / move.js
Created April 12, 2025 03:29
Movement Detector
class MovementDetector {
constructor(options = {}) {
// Configuration with defaults
this.threshold = options.threshold || 5;
this.debug = options.debug || false;
this.onMovementChange = options.onMovementChange || null;
// State tracking
this.lastRotation = { x: 0, y: 0, z: 0 };
this.isMoving = false;
@thyngster
thyngster / wpo.js
Created April 12, 2025 03:06
WPO Metrics Tracker
(function(){
var wpo = {
redirects: window.performance.navigation.redirectCount,
resources: {
iframe: 0,
img: 0,
css: 0,
script: 0,
beacon: 0,
xmlhttprequest: 0,
@thyngster
thyngster / cid_monitor.js
Last active April 12, 2025 01:15
Monitor GA Client ID
(function() {
let ga_cid = document.cookie
.split('; ')
.find(row => row.startsWith('_ga='))
?.split('=')[1];
// Listen for changes in the cookie store
cookieStore.addEventListener('change', (e) => {
e.changed.forEach(c => {
if (c.name.startsWith('_ga')) {
@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';