📜 ⬆️ ⬇️

We learn Google Analytics to consider likes from Vkontakte

Hello!

In this article I will tell you how to use Google Analytics to track likes on your site and show you how to do it with the “Like” button from Vkontakte.

On July 7 of this year, Google added to Analytics the ability to track the number of likes, anlays and any other retweets. A very logical action, given the popularity of these buttons and the launch of +1. This feature is called Social Plugin.

Unfortunately, in order to make it all work, it is not enough just to copy a couple of lines from the documentation to the code of your site, you also need to deal with the API buttons of each of the social networks. At this often all the joy of the news and ended.
')
To Google's credit, it should be noted that they made a whole separate website with examples of how to integrate the Social Plugin with the most popular networks in the West - Facebook and Google. Below I will explain how to integrate it with Vkontakte.

Let's start with a description of how the Social Plugin works. In order to enable this feature on your website, you need to use the _trackSocial function, the call of which looks like this:
_gaq.push([ '_trackSocial' , , , URL (-) , page path (-) ]);

The URL parameter is optional, if you omit it, Google Analytics will replace the current page address in its place. This parameter is needed if you want to see in the report some other page address or other information in general.

For example, you have the same article on your site available under two URLs (eg mypics.iss / news / 01-12-2028 / 1 and mypics.iss / articles / grandma_in_space ), then when you click like, the values ​​will be distributed at two different URLs, although in fact it’s the same page. To correct this misunderstanding in the URL parameter you need to write, for example

_gaq.push([ '_trackSocial' , 'facebook' , 'like' , 'http://mypics.iss/articles/grandma_in_space' ]);

By the way, this line does not have to be a link, you can write like this:

_gaq.push([ '_trackSocial' , 'facebook' , 'like' , 'grandma_on_ISS' ]);

By default, the relative page address is written to the page path parameter (in our case, this is /articles/grandma_in_space ) and it is not necessary to write it either.

Now that you understand how this works, you can move on to integration with social networks. Social Plugin by default tracks only Google+ and only if you do not have a counter name specified.

Let's consider how to configure integration using the example of VKontakte. First of all, we need to go to the VKontakte page for developers and learn how to interact with their button in general.

We see that if you have the “Like” button on your page, then the VK.Observer object becomes available, in which you can subscribe to the widgets.like.liked and widgets.like.unliked events .

There are no fundamental differences from Facebook, so we write in the image and example of Google:

 _ga.trackVkontakte = function(opt_pageUrl, opt_trackerName, opt_targetUrl) { var trackerName = _ga.buildTrackerName_(opt_trackerName); try { if (VK && VK.Observer && VK.Observer.subscribe) { VK.Observer.subscribe('widgets.like.liked', function() { _gaq.push([trackerName + '_trackSocial', 'vkontakte', 'like', opt_targetUrl, opt_pageUrl]); }); VK.Observer.subscribe('widgets.like.unliked', function() { _gaq.push([trackerName + '_trackSocial', 'vkontakte', 'unlike', opt_targetUrl, opt_pageUrl]); }); } } catch (e) {} }; 


Please note that I use the _ga variable from the Google example, so in order for this code to work, you need to add it, for example, to the end of the Google file.

Actually, that's all. Now it’s enough to put this code somewhere in the head, and after initializing the “I like” button add a call

 _ga.trackVkontakte(); 


As a result, the following lines will appear in the reports (available only in the new interface):

Source: https://habr.com/ru/post/129333/


All Articles