Get Google Analytics ID using Javascript
I am trying to extract google analytics id from html document.
I found the following function:
function get_UA() {
txt = document.getElementById('scripttag').value;
var matches = txt.match(/(UA-[\d-]+)/);
if (matches[1]) {
alert(matches[1]);
}
}
But I am getting this error:
TypeError: 'null' is not an object (Evaluate 'Document.getElementById (' scripttag '). Value')
Any ideas?
+3
source to share
3 answers
ga.getAll()[0].get('trackingId')
If you already have a tracker, you can get it using
tracker.get('trackingId')
Carefull , don't forget the best Google Analytics recommendations
Do not use ga object methods outside of readyCallback, as methods may not yet be available.
0
source to share