Last active
December 26, 2015 23:29
-
-
Save bouze/7231216 to your computer and use it in GitHub Desktop.
Facebook, Twitter, Google Analyticsをまとめる非同期スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script> | |
//analytics.jsが非同期で読み込まれるので先回りして配列にパラメーターを入れておく | |
window.ga = { | |
q: [ | |
['create','XX-00000000-0','domain.com'], | |
['send','pageview'] | |
] | |
}; | |
// FacebookのAPIが初期化された時に呼ばれる | |
window.fbAsyncInit = function() | |
{ | |
// LikeボタンのイベントをGAでトラックする | |
_ga.trackFacebook(); | |
}; | |
// TwitterのAPIが非同期で初期化されるので先回りして初期化のコールバックキューに並んでおく | |
window.twttr = { | |
_e: [ | |
function() | |
{ | |
// TweetボタンのイベントをGAでトラックする | |
_ga.trackTwitter(); | |
} | |
] | |
}; | |
var scriptSecond; | |
var scriptFirst = document.getElementsByTagName("script")[0]; | |
var loadScript = function(url, id) | |
{ | |
if (!document.getElementById(id)) | |
{ | |
scriptSecond = document.createElement("script"); | |
scriptSecond.src = url; | |
if (id) | |
{ | |
scriptSecond.id = id; | |
} | |
scriptFirst.parentNode.insertBefore(scriptSecond, scriptFirst); | |
} | |
}; | |
loadScript("//www.google-analytics.com/analytics.js", "ga"); | |
loadScript("//platform.twitter.com/widgets.js", "twitter-wjs"); | |
loadScript("//connect.facebook.net/ja_JP/all.js#xfbml=1&appId=000000000000000", "facebook-jssdk"); | |
</script> | |
<!-- このへん参照のこと → http://analytics-api-samples.googlecode.com/svn/trunk/src/tracking/javascript/v5/social/facebook_js_async.html --> | |
<script src="http://analytics-api-samples.googlecode.com/svn/trunk/src/tracking/javascript/v5/social/ga_social_tracking.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script> | |
(function(w,d){ | |
w.ga={q:[['create','XX-00000000-0','domain.com'],['send','pageview']]}; | |
w.fbAsyncInit=function(){_ga.trackFacebook();}; | |
w.twttr={_e:[function(){_ga.trackTwitter();}]}; | |
var s,e=d.getElementsByTagName("script")[0], | |
a=function(u,f){if(!d.getElementById(f)){s=d.createElement("script"); | |
s.src=u;if(f){s.id=f;}e.parentNode.insertBefore(s,e);}}; | |
a("//www.google-analytics.com/analytics.js","ga"); | |
a("//platform.twitter.com/widgets.js","twitter-wjs"); | |
a("//connect.facebook.net/ja_JP/all.js#xfbml=1&appId=000000000000000","facebook-jssdk"); | |
})(this, document); | |
</script> | |
<script src="http://analytics-api-samples.googlecode.com/svn/trunk/src/tracking/javascript/v5/social/ga_social_tracking.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment