2017-11-10 8 views
2

私は、環境に基づいて、ライブラリにアクセスする必要がある環境に基づいてdev、プリプロダクション、プロダクションなどの複数の環境を持っています アプリ負荷スクリプトタグライブラリを避ける方法ホストホスト上で使用するcdnリンク

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script> 

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script> 

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script> 

このLIBを使用しながら、ベースのホスト起源ID値を変更し、機能GTAG()は場合

window.dataLayer = window.dataLayer || []; 
    function gtag(){dataLayer.push(arguments)}; 
    gtag('js', new Date()); 

を呼ぶ私は(index.htmlのように機能GTAG 3つのスクリプトLIBSに言及しました)はlib datを呼び出します

+0

は、これまでのWebPACKを使用するのではと思った私のインデックスファイルをロードするときだから、動的に必要なIDとCDNのリンクを作成していますwindow.location.host;

 var host = window.location.host; var url; if (host.includes('dev-something.com')) { url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx'; } else if (host.includes('preprod-something.com')) { url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx'; } else if (host.includes('something.com')) { url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx'; } else { url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx'; } var head = document.getElementsByTagName('head')[0]; var theScript = document.createElement('script'); theScript.type = 'text/javascript'; theScript.src = url; head.appendChild(theScript); 

を取得使用して? –

答えて

3

私は、私は、スクリプトCDNリンクとの差分IDを配置する必要がある環境に基づいて開発し、事前PROD、生産などの複数の環境を持っている

ので、代わりのLIBを隠し、新しいスクリプトCDNのリンクを作成

関連する問題