PhearOfRayne PhearOfRayne - 1 year ago 75
Javascript Question

How to validate Google Analytics tracking id using a JavaScript function

I need to validate a Google Analytics tracking ID. So I created a simple function but my RegEx must be wrong because it just returns false everytime. I'm not sure if there is a set amount of digits in the middle or at that end of a Google Analytics ID so was just trying to just look for at least one digit.

JavaScript Function:

function validateGA(id) {
var regexp = /ua-\d+-\w\d+/;
return regexp.test(id);

Tested with
but just keep returning false.


Avi Avi
Answer Source

GA ID does not have a fixed number of digits in it. Also, There have been legacy ids that started with MO or YT etc. For your purposes, the correct regex could be something like:


This works for following scenarios:

UA-1234- // Invalid
UA--1   // Invalid

Also, To check this in jsfiddle:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download