Hello everyone,
PROBLEM
I have been using the forge component Google Analytics Reactive and have been getting the error "Error adding script 'https://www.google-analytics.com/analytics.js' to document 'https://xxx-xxxx.xxxx.xx/? ".
I saw that this question has come up before and people were pointing to ad blockers as a cause for this problem.
CAUSE
After taking some time to explore this issue, and with help of fellow developers, we found out that it can be ad blockers or the browser privacy settings blocking the script. As an example, here are Edge's privacy settings:
You can see that a strict setting blocks a majority of trackers. Google Analytics (GA) is part of that list. Ad blockers do the same, they also block GA.
ISSUES
The forge component, in its Initialize action ('Init'), uses the OutSystems action 'RequireScript' to add the GA script to the page head. It is in this moment that the error occurs "Error adding script 'https://www.google-analytics.com/analytics.js'" because the browser and the ad blockers do not allow to have that call.
When blocking GA every browser can react differently:
As we were exploring this problem and searching for workarounds we saw all three, with 2 and 3 being the worst scenarios.
SOLUTION BRAINSTORM
We thought of:
Our script
Website working with GA
It is this asynchronous and synchronous call that makes the difference, and it was the synchronous call that was making the error showing on the screen (2) or the page crashing (3):
POSSIBLE SOLUTION
As mentioned before, the forge component, in its Initialize action ('Init'), uses the OutSystems action 'RequireScript'. By default this action adds the required JS to the header as synchronous script.
So what we did was:
var head1 = document.getElementsByTagName('head')[0]; var new_script1 = document.createElement('script'); new_script1.type = 'text/javascript'; new_script1.charset = "utf-8"; new_script1.async = true; new_script1.src = "https://www.google-analytics.com/analytics.js"; head1.appendChild(new_script1);
RESULTS
When the strict policy and the ad blocker is on:
When the ad blocker is not on and the privacy policy is standard:
GOAL OF THIS POST
ALSO
From this we also learned that it would be nice to have a lot more control of the 'Require Script' action. As such I have given the idea to allow controlling if the required script is sync or async: https://www.outsystems.com/ideas/11797/upgrade-require-script-system-client-action/
THANK YOU
If you were able to read through all this... thank you for reading until the end. A big thank you to my fellow developers that helped with this.
FEEDBACK
What do you think of what we just shared? Any thoughts or improvement ideas?
We look forward to get your feedback.
____________________________________________________________________________________________________________________________
(previous forum posts)
https://www.outsystems.com/forums/discussion/75476/errorscreen-error-adding-script/
https://www.outsystems.com/forums/discussion/72827/google-analytics-reactive-google-analytics-reactive-javascript-is-being-blocked/
Hi,
I'm not very sure but google apps use google mediaapi, microapps to use JS