Skip to content

Instantly share code, notes, and snippets.

View mbaersch's full-sized avatar

Markus Baersch mbaersch

View GitHub Profile
@mbaersch
mbaersch / gms_wp_ga_postinfos.php
Last active August 21, 2022 20:02
Wordpress Plugin-Code zur Ausgabe von Infos zu Autor, Kategorie etc. als Variablen im dataLayer für den Google Tag Manager
<?php
/*
Plugin Name: PageInfos2GTM
Plugin URI: https://www.gandke.de/
Description: Nach Aktivierung werden Angaben zu Loginstatus Autor, Monat, Jahr, Kategorie und Tags als wp_utype, wp_aut, wp_month, wp_year, wp_cat und wp_tags - zusammen mit einem Event "wpPageInfo" - als Script im Footer von Seiten oder Beitr&auml;gen in den dataLayer geschrieben, um diese z. B. via Google Tag Manager in benutzerdefinierten Dimensionen zu nutzen.
Zudem wird der Seitentitel uebersetzungssicher als org_page_title im dataLayer konserviert.
Achtung: Das verwendete Template muss wp_footer() aufrufen (was aber die meisten tun sollten ;))
@mbaersch
mbaersch / gaproxy.php
Last active July 7, 2022 14:48
simple PHP proxy for Google Analytics hits
<?php
/******************************************************************************************************/
//send hits to GA- or GTM tag-server: use this file via transport_url
//more info: https://developers.google.com/tag-manager/serverside/send-data
/******************************************************************************************************/
//define destination for tracking hits - either on www.google-analytics.com
//or own tag server like https://trk.myserver.com/collect or
//https://gtm-xxxxxx-xxxx.xx.x.appspot.com/collect :
$endpoint = "https://www.google-analytics.com/collect";
@mbaersch
mbaersch / readingtime.js
Last active June 9, 2022 10:13
Lesezeit eines Blogbeitrags für GTM
function() {
//Hauptelement eines Blog-Contents selektieren
var txt = document.querySelector('#cmain').innerText;
var lng = txt.split(" ").length;
var l = lng / 300;
//300 Worte / Minute Lesezeit oder mindestens 30 Sekunden zurückgeben
lng = (lng == 0) ? 30 : l * 60;
return Math.round(lng * 1000);
}
@mbaersch
mbaersch / timed-visibility-observer.html
Last active June 9, 2022 10:12
Timed Visibility Observer - HTML-Tag für Piwik PRO Tag Manager zur zeitgesteuerten Auslösung von Visibility Trigger Events
<script>
//******************************************************************************************************
//HTML-Tag für Piwik PRO Tag Manager zur zeitgesteuerten Auslösung von Visibility Trigger Events.
//******************************************************************************************************
//Hinweise siehe https://gist.github.com/mbaersch/8cc6419f3a2b16f6c8641a828460d06e
//Optimierung:
//Die Funktion minimalReadingTime() kann entfernt werden, wenn feste Zeiten oder keine Mindestdauer
//benötigt wird. Ebenso kann dieser Kommentar entfernt werden, um nicht die Ladezeit des Tag Managers
@mbaersch
mbaersch / Universal-Export-R-Beispiel.R
Last active May 2, 2022 12:02
Beispielscript zum Export von Daten aus Google Universal Analytics mittels R
#---------------------------------------------------------------------------------
# Beispielscript: Export von Daten aus Google Universal Analytics mittels R
#---------------------------------------------------------------------------------
# Dieses Beispiel gehört zum Blogpost unter
# https://www.markus-baersch.de/blog/universal-analytics-daten-sichern-was-und-wie/
# Damit es nutzbar ist:
# - R installieren: https://cran.rstudio.com/
# - RStudio installieren: https://www.rstudio.com/
# - Dieses Script in RStudio öffnen
@mbaersch
mbaersch / getgtm.php
Last active March 18, 2022 01:42
Local caching helper for GTM containers
<?php
/******************************************************************
Helper for locally cached GTM container: update local GTM cache and
enable optional live container loading for debugging
******************************************************************/
//path and filename for local GTM container cache, relative
//to this PHP file or as absolute server path
$gtm_save_path = "../js/gtm.js";
@mbaersch
mbaersch / matomo-resize-tagcode.html
Last active February 19, 2022 12:12
adjust size of Matomo Tag Manager preview / debug console
<script>
//use this script to adjust size of Matomo Tag Manager preview / debug console
//usage: create html tag in MTM, paste this code and fire tag on DOM ready, if "Preview Mode" is "1"
var mtmPreviewSize;
//resize MTM preview via console
function resizeMtmPreview(prz) {
//only in preview mode
if ({{PreviewMode}}) {
var mtm = document.querySelector('iframe#mtmDebugFrame');
@mbaersch
mbaersch / functions.php
Created March 25, 2020 11:39
send serverside conversions from a Wordpress shop (e. g. wooCommerce) to Google Analytics
<?php
// add one of the two following options to the functions.php of your child theme in order to send server side
// conversion data to Google Analytics
// NOTE: function store_gclid() is used by both variants
/***************************************************************************************************/
// send conversion to Analytics - Option 1: track success page as only pageview in the session and
// create a goal for path "/conversion/"
// adjust goal path (fragment) in $goalurl in order to fit your url structure
// comment out line below to deactivate
@mbaersch
mbaersch / content-visibility-marker.html
Created November 23, 2021 13:13
Custom HTML Tag for Google Tag Manager to create markers in main content for triggering "content consumption" events
<script>
/*
This code can be used as a Custom HTML tag in Google Tag Manager to create invisible "marker lines"
every x percent inside a container (selected via CSS selector) that holds the main content of a page
(like a blog post).
You can then use visibility triggers for the marker lines (selected by name or class) in GTM in order
to fire event tags for Google Analytics or any other tool to measure "content consumption" without
having to rely inaccurate scroll tracking.
@mbaersch
mbaersch / cached_event_id.js
Last active November 4, 2021 09:41
getUniqueEventId() - creates cacheable event ids for deduplication of events that cannot use the same trigger
/* create unique event ids for specific events that "live" for a limited time
(default: 1 scond).
returns a value that consist of the key, a random number and a timestamp;
dot-separated. An existing value for a key that is requested within the
defined lifespan will remain the same, otherwise (not existing or too old)
a new value is generated and stored. The limited lifespan can be used to
deduplicate several hits of the same type between page loads (e. g. Add2Cart
or multiple PageViews in SPAs).