Skip to content

Instantly share code, notes, and snippets.

View MatthewDaniels's full-sized avatar

Matthew Daniels MatthewDaniels

  • Brisbane, Qld, Australia
View GitHub Profile
@MatthewDaniels
MatthewDaniels / reverse-url.gs
Last active October 12, 2016 01:31
Google App Script to reverse a url
/**
* Reverses the input url.
*
* EG: if the url is www.google.com, the result will be com.google.www - this is useful for sorting a sheet
*
* @param {string} input The url to reverse.
* @return The url reversed.
* @customfunction
*/
function REVERSEURL(string) {
@MatthewDaniels
MatthewDaniels / parse-query-parameters-into-a-map.js
Last active May 19, 2023 01:34
Parses a query parameter string into a javascript key-value map. This script deals with multiple values by placing them into an array
/**
* Get a query map based on a query string.
*
* The function will populate a map variable with key value pairs of the parameters.
*
* If there is more than one of the same key, the function will populate an array in the map with the multiple values within it
*
* @param {string} query The query string - the question mark is optional
* @return {object} key value pairs of the parameter / value of the parameter
*/
@MatthewDaniels
MatthewDaniels / Forward GA Hit to GTM via datalayer
Last active June 30, 2019 04:25
Sends any GA hits to GTM for extra processing. Put this file directly after the ga create call.
/*
This code should be put directly after the ga create call, before any hits are fired to ensure
it captures all hits being sent to ga.
When any ga hit is fired, this code will capture it, fire the proper hit into GA and then forward
the hit query parameters to GTM via a dataLayer event.
The dataLayer event is called "gaHitTask" and the query parameters are sent in the "gaHitTaskParams" variable.
You can use code something similar to this: https://gist.github.com/MatthewDaniels/388fa1e0c02613f103f00a504ed58c55
ga(function(tracker) {
// Grab a reference to the default sendHitTask function.
var originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function(model) {
// sends the original hit task
@MatthewDaniels
MatthewDaniels / GA-Management-Properties-and-lists.gs
Last active June 7, 2016 04:13
A simple google apps script script for Google Sheets that replaces the current sheet's data with Property & View information for a specific Google Analytics Account. You will need to enable access via the API to your account - see: https://developers.google.com/apps-script/guides/services/advanced ... NOTE: THIS IS QUITE MESSY AND HAS NO CHEKCIN…
function getSummary() {
var listed = Analytics.Management.AccountSummaries.list();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// var baseRangeRow = 4
// sheet.getRange(baseRangeRow,1).setValue(listed.items[0]);
// sheet.getRange(baseRangeRow+1,1).setValue(listed.items[0].webProperties[0]);
// sheet.getRange(baseRangeRow+2,1).setValue(listed.items[0].webProperties[0].profiles[0]);
@MatthewDaniels
MatthewDaniels / cache.conf
Created May 20, 2016 01:31
Nginx Standard Cache Conf: Store this file in /etc/nginx/conf.d and include in standard server directive
# Proxy Cache Settings
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;
@MatthewDaniels
MatthewDaniels / gzip.conf
Last active May 1, 2021 01:01
Nginx Standard Gzip Conf: Store this file in /etc/nginx/conf.d and include in standard server directive
# Compression
# Enable Gzip compressed.
gzip on;
# Enable compression both for HTTP/1.0 and HTTP/1.1.
gzip_http_version 1.1;
# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
@MatthewDaniels
MatthewDaniels / GA Tracker helper
Last active November 13, 2017 03:47
This script iterates the Google Analytics trackers on the page (assuming the Google Analytics variable is 'ga'. It then outputs various bits of info, including the tracker name, the tracking ID, the client ID, the cookie domain and any custom dimensions that exist. Copy and paste it into the console on the page you want to find out more about.
if(window.ga) {
var arr = window.ga.getAll();
for (var i = 0, len = arr.length; i < len; i++) {
var t = arr[i];
if(console.group) {
console.group(t.get('name'));
}
console.log('Main tracker object:', t);
@MatthewDaniels
MatthewDaniels / parseQueryString.js
Created April 6, 2016 03:29
Parse the current window location's query parameters into a map
function parseQueryString() {
var query = (window.location.search || '?').substr(1),
map = {};
query.replace(/([^&=]+)=?([^&]*)(?:&+|$)/g, function(match, key, value) {
(map[key] = map[key] || []).push(value);
});
return map;
}
@MatthewDaniels
MatthewDaniels / getIPForGTM.js
Created March 16, 2016 21:52
Load a user's IP address via a service so we can add it to a dataLayer in GTM - beware we do not want to store it in it's raw form, but we can manipulate the data in GTM once there (ie: check against a filtered list and add a flag)
var request = new XMLHttpRequest();
// if the endpoint changes, then manipulation of the returned data may need to change in the onReadyState handler
request.open('GET', 'https://api.ipify.org', true);
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
// Success!
var data = this.responseText;
console.log('respose', data);