2013-02-08 19 views
3

我試圖從html文檔中提取Google分析ID。使用Javascript獲取Google AnalyticsID

我發現了以下功能:

function get_UA() { 

txt = document.getElementById('scripttag').value; 

var matches = txt.match(/(UA-[\d-]+)/); 

if (matches[1]) { 

    alert(matches[1]); 

} 

} 

但是我得到這個錯誤:

TypeError: 'null' is not an object (evaluating 'document.getElementById('scripttag').value')

任何想法?

回答

6

你能訪問_gaq變量嗎?如果可以的話,頁面使用異步跟蹤...

var accountId = _gaq._getAsyncTracker()._getAccount(); 
+0

+1這正是我一直在尋找的來源:) – VisioN

+1

對於新的萬用谷歌分析(https://support.google.com/analytics/answer/2790010?hl = en)其中'_gaq'已被翻譯爲'ga',您可以使用:'ga.getAll()。shift()。b.data.w [':trackingId']' – sospedra

1

在2017年4月,這個工程:

ga.getAll()[0].b.data.values[':trackingId']

0
ga.getAll()[0].get('trackingId') 

如果你已經有了一個跟蹤器,你可以得到它與

tracker.get('trackingId') 

小心,不要忘記google analytics best practices

Don't use ga object methods outside a readyCallback as the methods may not be available yet.