It looks like twttr is not yet initialized. Make sure the script ( //platform.twitter.com/widgets.js ) loads before the code block that you have.
That is why you get undefined (twttr).
After viewing your changes, itβs very clear what is happening. Your scripts add a head and load scripts after the page loads. Right after you execute the code, which depends on what you put in your head and is still loading, so twttr is not yet initialized.
Try this code below:
window.addEventListener("load", function() { window.fbAsyncInit = function() { FB.Event.subscribe('edge.create', function(targetUrl) { ga('send', 'social', 'facebook', 'like', targetUrl); }); FB.Event.subscribe('edge.remove', function(targetUrl) { ga('send', 'social', 'facebook', 'unlike', targetUrl); }); } document.getElementById('tweetjs').addEventListener('load', function() { twttr.ready(function (twttr) { twttr.events.bind('tweet', function(e){ if(!e) return; ga('send', 'social', 'twitter', 'tweet', theURL); }) }); }, false); }, false);
If you need cross-browser support, you can follow what they did in their function to check window.addEventListener first and then go back to window.attachEvent for older browsers.
user2010925
source share