问题
I want to track some actions in Google Analytics but I can't get it working.
I tried it with Contact Forms (Wordpress). At the additional settings I just wrote:
on_sent_ok: "ga('send', 'event', 'Formular-Beratung', 'sent');"
but it does not work. I installed the Google tag assistant, but I cant see any event (not even in the live view at Google Analytics).
Next:
There is a live chat (snapengage). Here you can also track events. For this I implemented the code
var seAgent;
SnapABug.setCallback('StartChat', function (email, msg, type) {
seAgent = agent;
ga('send','event','SnapEngage', 'hasChatted', email);
});
I got this from there official site: see here
This code should send an event to Google, when someone is sending a message in the chat), but nothing happens.
It seems like the whole site does not send ANY events to Google.
This is the site I am talking about. Please could anybody have a look at this?
回答1:
You need to make sure that you initialize the tracker object before you call the ga()
method. You do this by placing this code at the top of the source code of your page (right after the <body>
element:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-Y', 'auto');
</script>
Make sure to replace the UA-XXXXXXX-Y with your own property ID.
来源:https://stackoverflow.com/questions/43957553/wordpress-and-google-analytics-events