2010-06-22 100 views
1

我正在使用Google Analytics的新異步代碼,在同一頁面調用兩次GA.js是否正確,如果不是,有沒有辦法做到這一點?谷歌分析異步代碼

我的問題是我需要在使用閃光燈的預訂表單上記錄兩個PageViews,問題在於用戶可能不需要進入第二階段,但在第一階段關閉窗口。

<!-- Step one, I need to record a pageview --> 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-6173481-3']); 
    _gaq.push(['_trackPageview', '/Step1']); 
    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

    <!-- Step two of the process i define a function that will get called by flash and record another pageview--> 

    function completed(){ 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-6173481-3']); 
    _gaq.push(['_trackPageview', '/Step2']); 
    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

    } 

回答

1

下面應該工作:

<!-- Step one, I need to record a pageview --> 
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-6173481-3']); 
    _gaq.push(['_trackPageview', '/Step1']); 
    (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 

    <!-- Step two of the process i define a function that will get called by flash and record another pageview--> 
    function completed(){ 
    _gaq.push(['_trackPageview', '/Step2']);  
    } 

然後從閃存需要時調用completed()