As a result, I will get some unexpected values in this variable. Let’s go ahead and save this and see what value gets assigned to this variable. I’m going to call this as cjs_ErrorTest. Go ahead and save it. Now our variable is created. Go ahead and put the tag assistant in preview mode. Click on preview, click on connect, and tag assistant is connected. Go to tag assistant, click on continue, go to container loaded because that’s where all the variables are being set. Click on variables, and here cjs_ErrorTest is showing you undefined. So clearly, there is no value coming across; that’s why it’s undefined.
To fix that, we will have to write some extra code. Go back to Google Tag Manager and write that code. Disconnect the tag assistant and go back to Google Tag Manager. Go down, find the variable (cjs_ErrorTest) and click on it. To find the actual error or to capture the error, you have to wrap this code in a try-catch statement. This statement starts with the keyword “try,” followed by open curly brackets, and go all the way down and add a closed curly bracket. So all of your code right here is wrapped in curly brackets.
Then you enter catch and pass in a variable (e) in the parenthesis. This variable can be named anything. This variable or identifier will contain the information about the error. After the variable, add an open curly bracket near the variable and then close the curly brackets below. Now, when an error occurs in this block, it will catch the block. Right here, you can return a message (return Error Occurred;) that will tell that an error occurred. When there is an error or exception in this code, this message will be returned. However, this message is not very meaningful. To provide a more meaningful message, you can use the e variable (return e.message;) here, also called an identifier, and use the message property of this identifier. This will give us a more meaningful message rather than a generic message.
Go ahead and save the variable, and now the variable is ready. Go and put our container in preview mode to see the value that’s contained in the variable. Click on preview, click on connect, tag assistant is connected. Go to the tag assistant window, click on continue to go to container loaded because that’s where you’ll see the variables. Click on variables and scroll down, and here you can see cjs_ErrorTest shows that var1 is not defined. That’s the problem in the code. Once you fix this error and go back to the preview mode, you’ll see the next error if there is one. So by using the error identifiers message property, you have been able to get a more meaningful error message.
- Workshop – We offer onsite customized workshops. Reach out to us for more details.
- Consulting – We can get you up and running with Google Analytics 4 as well as train your and your team to use it effectively. Reach out to us for more details.