The issue you're facing is due to the code expecting a method that is not within scope. By making a slight modification to the code, you can eliminate this error. The problem can be resolved by using the following code snippet: http://pastebin.com/RrQ2848j
It's important to note that I am simply returning the callback function and assigning it as a variable. While there are other approaches you could explore, there must be something in the global scope to call.
This occurs because a script block is created to retrieve script and data, as an AJAX(XHR) request would breach the same-origin policy if attempting to access google.com from yourdomain.com. Once the script is downloaded, it expects to call a function in the global scope to pass some data into it. This function is specified on the query string of the SRC attribute when creating the script block, as demonstrated below:
function runPagespeed() {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
var query = [
'url=' + YN_URL,
'callback=runPagespeedCallbacks',
'key=' + API_KEY
].join('&');
s.src = API_URL + query;
document.head.insertBefore(s, null);
}