matomen
(A)
July 27, 2022, 9:26am
1
I’m having issues showing the debug mode on a specific website. I’ve been implementing Matomo on other websites at the same time and don’t have the issue. I’ve published all versions and still have same issue.
In the console of the website I did see an error:
Uncaught SyntaxError: Unexpected token ‘{’
Anyone know how this can be fixed?
Lukas
(Lukas Winkler)
July 27, 2022, 10:56am
2
This sounds a lot like
opened 09:32PM - 06 Jan 20 UTC
Enhancement
# Summary
Using an existing variable into a Custom JavaScript variable can av… oid a lot of dev and can be really useful.
Example: you wish return a custom attribute from current clicked element. Use `{{ClickElement}}` is essential.
## Current behavior
Into a custom javascript variable, add the `{{ClickElement}}` variable like :
```javascript
function () { if ({{ClickElement}}) { return {{ClickElement}}.getAttribute('my-attribute'); } else { return ""; } };
```
This code return the following error:
<img width="985" alt="Screen Shot 2020-01-06 at 16 09 16" src="https://user-images.githubusercontent.com/15279205/71849149-5576be00-309f-11ea-8eb1-23a0284a7d3f.png">
<img width="357" alt="Screen Shot 2020-01-06 at 16 09 33" src="https://user-images.githubusercontent.com/15279205/71849153-57408180-309f-11ea-8225-7d0eced7917d.png">
Moreother, when this kind of error occured, the Preview mode break totally 😕
Maybe a similar issue here: https://forum.matomo.org/t/how-to-refer-user-defined-variables-in-custom-javascript/34932
## Expected behavior
Add a variable into a custom javascript variable
(What you enter as a custom JS function needs to be valid JS and not contain any placeholders)
matomen
(A)
July 27, 2022, 1:29pm
3
Uh oh, that does sound like the case. I did use a placeholder in a variable with custom JS.
I changed it to a value as a test and tried to debug (after clearing cache) and it works now…
Meaning I’d have to use a JS to get value from the dataLayer in my case then instead of using existing variable/placeholder…
No update on this addition then to the JS variables?
The workaround is described there:
You can simply use the TagManager object, e.g.
function () {
if (TagManager.dataLayer.get('mtm.clickElement') && TagManager.dataLayer.get('mtm.clickElement').getAttribute('data-name')) {
return TagManager.dataLayer.get('mtm.clickElement').getAttribute('data-name');
} else {
return "";
}
}
Will store the value of the attribute “data-name” of the clicked element.
matomen
(A)
August 8, 2022, 6:50am
6
yeah in my case I had to remove the placeholder in the custom JS variable.
Instead I used javascript directly in my custom HTML tag to get the JS variable. Hope this helps for others!