How to track 404 errors in Google Tag Manager
source link: https://barryvanveen.nl/articles/27-how-to-track-404-errors-in-google-tag-manager
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to track 404 errors in Google Tag Manager
Jul 03, 2016
Google Tag Manager (GTM) enables you to track different events by defining variables, triggers, and tags. In this post, I explain how to track 404 errors using the DataLayer.
I assume you already have GTM installed and verified that it is working. If you haven't done this, follow the setup instructions first.
Setting up your application
Setting up GTM in Laravel is possibly even easier than the manual installation. The people of Spatie have released a package to install GTM on your website and added a great way to configure the DataLayer variable.
Install spatie/googletagmanager using the documentation on the GitHub repository. Make sure to configure the GTM id from your environment configuration file, do not hard code it into config/googletagmanager.php
. Read more on the proper use of configuration files in Laravel if you are not sure how to do this.
When you installed everything, you are able to set a variable in the DataLayer with GoogleTagManager::set('foo', 'bar')
. In my ExceptionHandler I assign a errorcode
variable containing the HTTP error code that was triggered, eg. GoogleTagManager::set('errorcode', '404');
. Take a look a the full ExceptionHandler for more details.
Configure GTM
Now it's time to login into Google Tag Manager and configure our variables, triggers and tags:
Make a new user-defined variable called "errorcode".
Create a new DataLayer variable
Set an empty default value (ie. "") so that you can be sure the variable exists on every page.Create a new trigger called "Error code on page".
Create a trigger for when the page contains an error code
This trigger will, ehm, trigger when theerrorcode
variable is non-zero.Add a new tag called for tracking events when there is an errorcode on a page.
Create a tag to track events
This tag uses our new variable as the name of our event. It is only called when our newly defined trigger matches.
The result
The result is that Google Analytics events are fired when an errorcode is found on the page. The category of the event will be the errorcode itself (eg. 404), the action of the event is equal to the URL that triggered the error and the label is equal to the referer URL.
Related articles
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK