Skip to content

Instantly share code, notes, and snippets.

@robhammond
Last active December 14, 2022 06:58
Show Gist options
  • Save robhammond/3832e4ef1db3f6e27f69623b6fd00a43 to your computer and use it in GitHub Desktop.
Save robhammond/3832e4ef1db3f6e27f69623b6fd00a43 to your computer and use it in GitHub Desktop.
Puppeteer script to check that Google Analytics Client ID is consistent across different platforms
'use strict';
const puppeteer = require('puppeteer');
// Test scenarios
// 4 different entrypoints
// ~8 different cookie setting scenarios (ie every page has at least 1 video)
// 2 different pre-set options - cookies and cookie-less
const reqUrls = [
'https://www.mirror.co.uk/comm-part-test/brightcove-lead-11944737',
'https://www.mirror.co.uk/comm-part-test/brightcove-lead-11944737.amp',
"https://www-mirror-co-uk.cdn.ampproject.org/c/s/www.mirror.co.uk/comm-part-test/brightcove-lead-11944737.amp",
'https://www.liverpoolecho.co.uk/comm-part-test/brightcove-lead-14226081',
'https://www.liverpoolecho.co.uk/comm-part-test/brightcove-lead-14226081.amp',
'https://www-liverpoolecho-co-uk.cdn.ampproject.org/c/s/www.liverpoolecho.co.uk/comm-part-test/brightcove-lead-14226081.amp',
'https://www.mirror.co.uk/3am/video/superstar-singer-hrvy-talks-halina-11047508?widgetId=6933150', // FBIA video widget
];
(async() => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setRequestInterception(true);
// set user agent to mobile phone
await page.setUserAgent('Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36');
// test setting pre-defined cookie
// page.setCookie({
// name : '_ga',
// value : 'GA1.3.57927592.1517247373',
// domain : '.mirror.co.uk',
// path : '/',
// expires : 1577836801
// });
page.on('request', request => {
const url = request.url();
// look for tracking script
if (url.match(/^https?:\/\/www\.google-analytics\.com\/(r\/)?collect/i)) {
// ignore requests to the wrong GA profiles
if (url.match(/ua-110513849/i)) {
let query = url.match(/\?v=1(.*?)$/)[1];
let params = decodeURI(query).split('&');
for (let i=0;i<params.length;i++) {
let key = params[i];
// log if client ID, type, or event action
// if (key.match(/^(tid|cid|cd2|t|ea)=/)) {
// console.log(key);
// }
if (key.match(/^(cd2|t)=/)) {
console.log(key);
}
}
console.log('---------------------');
}
}
// look for call to CID server
if (url.match(/^https?:\/\/stmg-prod\.mirror\.co\.uk\/analytics\.config\.json/i)) {
console.log("STMG endpoint called\n---------------------");
}
request.continue();
});
try {
// randomise entrypoints
var randReqUrls = shuffle(reqUrls);
for (var i = 0;i<randReqUrls.length;i++) {
console.log("-------------------------\nFetching " + randReqUrls[i] + "\n-------------------------");
await page.goto(randReqUrls[i], { waitUntil: 'networkidle2' });
await page.waitFor(1000);
}
} catch (err) {
console.log("Couldn't fetch page " + err);
}
await browser.close();
})();
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment