1

Google アナリティクス API のテスト中に JavaScript を遅延読み込みしています。サーバーでホストされているいくつかの静的 JS ファイル (googleAnalyticsAuthorization_v3.js や googleAnalyticsApi_v3.js など) があり、それらを適切にクエリ/評価できますが、次のようにクエリ/評価する方法がわかりません: http://apis. google.com/js/client.js?onload=handleClientLoad

私の AJAX ローダーは簡単です。

function requestJavascriptWithHttpMethod(filePath, httpMethod)
{
    request = new XMLHttpRequest();
    request.open(httpMethod, filePath, true);

    request.onreadystatechange = function()
    {
        _log('Request "'+filePath+'": readyState <'+requestReadyStateString(this.readyState)+'>, status <'+requestStatusString(this.status)+'>.');      

        if (this.readyState == this.DONE &&
            requestStatusString(this.status) == 'OK')
        {
            _log('Loading of "'+filePath+'" finished.');
            eval(this.responseText);
        }
    }

    request.send(); 
    _log('Request "'+filePath+'"...');
}

function requestJavascript(filePath)
{ requestJavascriptWithHttpMethod(filePath, "GET"); }   

URL を投稿しようとしましたが、結果が実行されませんでした (実際にはステータス コード 0 で返されます)。

requestJavascript('googleAnalyticsAuthorization_v3.js'); //Loads, evaluates well.
requestJavascript('googleAnalyticsApi_v3.js'); //Loads, evaluates well.
requestJavascriptWithHttpMethod('http://apis.google.com/js/client.js?onload=handleClientLoad', "POST"); //Nothing seems happening, returns with status code 0.

次のように、クライアントの HTML コードに単純に含めたのと同じように動作する必要があります。

<script src="http://apis.google.com/js/client.js?onload=handleClientLoad"></script>

コンソール出力は次のとおりです。

Request "googleAnalyticsAuthorization_v3.js"...
Request "googleAnalyticsApi_v3.js"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad": readyState <request finished and response is ready>, status <0>.
Request "googleAnalyticsApi_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsApi_v3.js" finished.
googleAnalytics_v3.js evaluated
Request "googleAnalyticsAuthorization_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsAuthorization_v3.js" finished.
googleAnalyticsAuthorization_v3.js evaluated 

ここで誰か助けてくれませんか?

実際には、すべてのロジックを JavaScript にカプセル化し、HTML 側に依存しないようにしたいだけです。

4

1 に答える 1

0

オリジンポリシーが同じであるため、サードパーティのドメインに対して Ajax 呼び出しを行うことはできません。通常、次を使用して JavaScript をページに追加します。

var script = document.createElement("script");
script.src = "foo.js";
document.getElementsByTagName("body")[0].appendChild(script);

これはクロスブラウザで機能しますが、問題は、Google アナリティクスがdocument.writeページに新しいスクリプト タグを追加するために使用することです。これは、すべてのページ コンテンツを置き換えることを意味します。その結果、ページの読み込み後にコードをページに動的に追加することはできません。

非同期読み込みについては、Google のサイトを参照してください: https://developers.google.com/analytics/devguides/collection/gajs/asyncTracking

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (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);
  })();

</script>
于 2013-01-08T14:42:48.217 に答える