Below is some code that can be used to help provide some insight into how to test a GA filter real time with the chrome developer console. This was the example code provided by Stephen Hamel in response to a question I posted in the Google Analytics Google+ Community. The goal of this code was to extract the pageID and pass it as the pagename alongside the page title.
var url = document.URL;
var B = ["",document.title];
var A=url.match(/(.+)pageid=([^]+)(.*)/);
console.info(A[1]+"pagename="+B[1]+A[3]);
The following can be used as a custom javascript macro to extract the last piece of the URL path from the referrer. I will typically use something like this code for form submission thank you pages to identify the previous page page and pass it to a label for GA event tracking.
function(){
var referrerPath= document.referrer.split('/');
var endPath = referrerPath[referrerPath.length-2];
if (document.referrer.split('/')[2]!=document.location.hostname)
{
return 'external';
}
else if (referrerPath.length<=4)
{
return '/';
}
else
{
return endPath;
}
} |
function(){
var referrerPath= document.referrer.split('/');
var endPath = referrerPath[referrerPath.length-2];
if (document.referrer.split('/')[2]!=document.location.hostname)
{
return 'external';
}
else if (referrerPath.length<=4)
{
return '/';
}
else
{
return endPath;
}
}
The following code can be used as a blocking rule so that any all pages tag that this is applied to will only allow the code to fire once.
//Once per Session Checking
<script>
var key_value = "sessionStatus=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
// Walk through the array
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
// Remove any leading spaces
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
// Look for cookie set by key_value
if (checkCookie.indexOf(key_value) == 0)
{
// The cookie was found so set the variable
foundCookie = 1;
dataLayer.push({'status': 'blocked'});
}
}
// Check if a cookie has been found
if ( foundCookie == 0)
{
// The key_value cookie was not found so set it now
document.cookie = key_value;
}
</script> |
//Once per Session Checking
<script>
var key_value = "sessionStatus=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
// Walk through the array
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
// Remove any leading spaces
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
// Look for cookie set by key_value
if (checkCookie.indexOf(key_value) == 0)
{
// The cookie was found so set the variable
foundCookie = 1;
dataLayer.push({'status': 'blocked'});
}
}
// Check if a cookie has been found
if ( foundCookie == 0)
{
// The key_value cookie was not found so set it now
document.cookie = key_value;
}
</script>
Just another WordPress site