以下のスクリプトを、翻訳スニペットと一緒にページに含めます
/*!
* Capture Analytics for Google Translate
* As of 2016, the Google Translate toolbar still only works with the old-style analytics (ga.js)
* The code below mocks the old analytics object, captures the events and passes them to the new Universal Analytics (analytics.js)
*
* Source: http://webmasters.stackexchange.com/a/101787/18749
* Copyright (c) Simon East 2016, for yump.com.au
* Free to use under MIT licence <https://opensource.org/licenses/MIT>
*/
window._gaq = {}; window._gat = {};
window._gat._getTracker = window._gat._getTrackerByName = function(){ return {
_trackEvent: function(eventCategory, eventAction, eventLabel) {
// [0] will send the event to the first analytics ID on the page (in case you have multiple)
if (window.ga && ga.getAll()[0]) {
ga.getAll()[0].send('event', eventCategory, eventAction, eventLabel);
window.console && console.log('Translation event sent to Google Analytics:', eventCategory, eventAction, eventLabel);
} else {
window.console && console.warn('Could not locate Google Analytics when attempting to log translation events.')
}
}
}}