I recently noticed that an event tracking of Google Website Translator is no longer available.
What I was changed is the code of Google Analytics: old ga.js code to new Universal Analytics code, analytics.js.
It seems Google Translate code is not updated for Universal Analytics. Here is my attempt to fix it by modifying core codes of Google Translate.
Put Google Website Translator in your blog
You can easily put Google Website Translator by following the instruction on the page.
You can also put your Analytics code to track translation events by Google Analytics.
Events have information of which languages are used in Event Label.
It had worked for a long time, but suddenly, it stopped working. It seems it stopped when I updated some codes of JavaScript, especially I updated Google Analytics code from old ga.js to Universal Analytics analytics.js.
I found some posts related to this problem:
The first one was posted in September/2013, and the latter was posted on 22/January /2015. So, it seems this problem is a remaining problem since Universal Analytics was released.
How to fix
From Google Website Translator, codes like below are provided:
1 2 3 4 5 |
|
Then, let’s check element.js:
http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit (Beautified element.js)
It calls main.js:
https://translate.googleapis.com/translate_static/js/element/main.js (Beautified main.js)
If your browser’s setting is other than English, a direct access to element.js shows different codes comparing to the Beautified code.
For example, with Japanese environment, element.js calls main_ja.js instead of main.js
https://translate.googleapis.com/translate_static/js/element/main_ja.js
In the below, element_main.js is common for all language, while such 0/main.js is replaced by 0/main_ja.js in Japanese environment.
It is too tight to follow all scripts. Therefore, in this attempt, I use only main.js from element.js. As a result, the translator button will be always English version.
Anyway, main.js calls:
https://translate.googleapis.com/translate_static/js/element/0/main.js
or
https://translate.googleapis.com/translate_static/js/element/26/element_main.js
Let’s check these scripts precisely. Hereafter, I would use 0_main.js for above element/0/main.js to distinguish from element/man.js.
In the beautified code, you can find following part in 0_main.js:
1 2 3 |
|
So, it uses only functions of _gaq
or _trackEvent
of ga.js.
As a result, we can not make an event with Universal Analytics.
Ok, now the place at where the event is created becomes clear. Then, it should work if the event track function of Universal Analytics is added by following my previous post: Notice a copy event on your blog in Google Analytics, like:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
p
and f
are Window
object in 0_main.js and element_main.js, respectively.
Window
should have ga
if analytics.js is used on the page (in the normal way),
instead of _gaq
of ga.js.
a.b
and a.e
have values of the language, so it should be in the 5th argument (Event Label).
In element_main.js, Zi
and Rj
are Google Website Translator
and Translate
, respectively.
So in both files, ga
works in the same way.
Once modifications are finished, put them in /javascripts/translate/. (In Octopress, put them in source/javascripts/translate/.)
Put 0_main.js as /javascripts/translate/0_main.js.
Next, modify:
as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
and
as
1 2 3 4 5 6 7 8 9 10 |
|
In the last, update the original Google Website Translator code in <head>
:
1 2 3 4 5 6 7 |
|
to call the local scripts.
(In the modified element.js, googleTranslateElementInit
is already set. (if you started from above beautified code.))
Summary
Now I can see translation events.
It may be better if I can fix with short codes from outside, but it seems difficult to do so, then got all scripts and modified locally.
This problem was posted in Google Group of Analytics, so I think Google engineers will notice and fix it soon.
Until then, I will keep this fix to track translation events.