How to catch 'ga is not defined' in your dev environments - javascript

So I have my development environment, and every time I run the project, I have a javascript error in my console:
ga is undefined
Of course I am not at the correct domain so Google Analytics see this as an issue and I am fine with that, but it doesn't allow the rest of my Javascript to run so I have modals and other JS based functionality that stops working.
My thought were I could just write a wrapper method and check for ga undefined. But before I do that I just wanted to see if there was a better solution or possibly different way of thinking about my setup.
Cheers,
Devin

I solved my issue with a method of redefining ga rather than trying to catch the error. In the backend we had logic to only included the Google Analytics script tag if the host url included our domain so when it was localhost or a dev url the script was not included.
So as a resolution I took the answer from this SO question ( Google Analytics Tag for Dev vs. Prod ) and implemented a similar solution in the backend instead and excluded the following code when not our production website domain.
ga('create', 'UA-********-1', 'mywebsite.com');
The result, ga was still defined but does not send data to our Google Analytics account.
Good Luck,
Devin

Related

analytics.js net::ERR_CONNECTION_REFUSED

Could anyone help me understand why this happens with google analytics code?
This is the error which then gives me to load the tracking code:
https://www.google-analytics.com/analytics.js Failed to load resource: net::ERR_CONNECTION_REFUSED
Is there any solution to this? I was trying to fix it all morning.
My situation:
If this error occurs, the tracking code does not work.
So if events are monitored and callback functions specified in hitCallback property are never executed. When the causes a deficiency in the functionality of the website and also in tracking statistics.
Types of events that have failed me: behavioral events, events advanced electronic commerce (step forward cart).
My attempts:
I have disabled antivirus, antivirus firewall, Windows firewall and defend unsuccessfully thinking that could be.
I installed the extention to debug google analytics. Googled for hours. I tried loading the code directly in the browser and on any pc I can get it. I could only get https://www.google-analytics.com/analytics.js making a wget from my server console.
It could be a problem with the ISP?
Thank you very much!
Update:
Confirmed is the ISP.
Although they have not solved in my case because the technician went on vacation.
If someone would come to have this problem, it occurs to me as a solution, edit the code tracking Analitycs to call a url eg "mydomain.com/analytics.js" it processor by modrewrite, and direct this request php file to download the actual file Js www.google-analytics.com/analytics.js and return in the response. As a proxy php. It's just an idea I have not tested.
Thank you all, and may close this question if you want.
It can be that you are using a VPN(Virtual private network) which analytics blocks. Try loading https://www.google-analytics.com/analytics.js
If it does not work, try disabling the VPN and try again
Check your hosts file (on mac go finder > go > go to folder and paste there /etc/hosts | on windows search), in my case www.google-analytics.com was blocked in hosts file.

Angulartics not tracking at all

I'm using angulartics for my application to track virtual page views on Google Analytics. This should be enabled by default but no visit to the page is tracked at all.
I should say that I wrapped the Google Analytics js code in a angular directive to be able to turn analytics off on my test server.
Otherwise its pretty standard, I included the angulartics source files in my index.html, added the module to my app.js and removed the ga('send', 'pageview') from the Google Code (as it is described in the docs).
I also found this hard to test. Should I see REST calls in the network console to verify its working? Because I'm not seing any (except the analytics code being loaded from the Google server).
Any idea what I am doing wrong?
Thanks,
Flosky
[edit]I should probably also mention that I'm using the angular ui router and not the default one[/edit]

Test Google Analytics on Localhost

I need to test some custom firing events using Google Analytics. I'd like to test in a local environment.
I've set up my test site using apache with server name: my-ga-test.localhost/
According to Can you test google analytics on a localhost address? and Google Analytics docs, I'm able to adjust my GA code to use:
ga('create', 'UA-XXXX-Y', {
'cookieDomain': 'none'
});
I tried to set up a new account to get a tracking ID for this site, but I'm getting the following error:
URL ends with an invalid top-level domain name
How do I generate the code using localhost domain so I can alter the tracking ID code to put on my localhost?
The "Website URL" field when creating a new Google Analytics property does not have to be the same as the actual URL that's used in production. It's primarily used for resolving relative links, not for validating hits.
Just change it to something else (like example.com), and everything should work fine.
I do not recommend the above answer.
I believe the best practice is to use the Analytics Debug js on your localhost. Debug prints in console EVERYTHING is doing, including firing events. That way you can check if your code is working or not without sending tracking info to Analytics on Localhost (which makes no sense).
Find out more here :)

How to fix Google Analytics "analytics.js" net::ERR_FILE_NOT_FOUND?

The relevant line of the Google script is:
(window,document,'script','//www.google-analytics.com/analytics.js','ga');
I am running this particular file locally (as in, I double-click "index.html" on Windows Explorer and it opens in Chrome.
After about a minute of trying to load this file it fails with net::ERR_FILE_NOT_FOUND reported in the Developer Console.
I understand why this is happening, it is trying to load this as a local file. If I do this it works just fine:
(window,document,'script','www.google-analytics.com/analytics.js','ga');
In other words, I eliminated the //. This would work just as well:
(window,document,'script','http://www.google-analytics.com/analytics.js','ga');
Is going explicitly doing it this way to only track real websites?
If so, how are you dealing with this issue for local development?
I do run a VM with Ubuntu server for local development of more elaborate sites. In this case I was working on a single file landing page that I knocked out quickly and wanted to just test just as easily but ran into the browser hanging waiting for analytics.js
Is it safe to remove the // or add http: in general terms. I don't generally like to modify Google code unless I fully understand why they are doing things in a specific way and what the consequences of my actions might be.
Instead of viewing the file locally, run it through a local webserver. Two easy options are
XAMPP: http://portableapps.com/apps/development/xampp
Uniform Server: http://www.uniformserver.com/
Both require no installation. If you have Visual Studio or WebMatrix, you can also run it through Cassini or IISExpress.
It can be that you are using a VPN(Virtual private network) which analytics blocks. Try loading https://www.google-analytics.com/analytics.js
If id does not work try disabling the VPN and try again.
Or as someone else mentioned it could be that your AdBlock is blocking it.

Google Maps Remote .js and Access-Control-Allow-Origin

I wrote a Google Maps API wrapper in JS, did some local tests with static html, and everything worked just fine. Then I loaded the files into a local web server running in localhost:8080, tested the map panels, and once again, everything worked just fine.
Then a week went by, I added a map to a page, and I couldn't get it to load. Nothing has changed (that I know of anyway, obviously something has), and static tests continue to work just fine, but when I try to load the API from the server I get this in the debug console:
XMLHttpRequest cannot load http://maps.google.com/maps/api/js?sensor=false.
Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
I read a bit around, but I still can't understand the error, much less fix it. Can someone please give me a hand?
edit: I use a simple tag to load google's js. No jQuery, no nothing.
Thanks in advance!
#Santiago hopefully I can help you now :) The error you are receiving is due to Google's prohibiting of cross site scripting. You can find the info here: Google which also references:Wikipedia Article
It looks like you'll need to create a proxy service for your client on your public facing webserver. Since the request comes from your public webserver and the reply goes back to your webserver it will meet the same domain requirements that Google requires. I do not know enough python yet to create a pythonic cgi proxy but I have to think that there are many different solutions out there already. Plus you will be limited by your server as to what type of solution you employ.
HTH!
~MWR

Categories

Resources