C
C
CMTV2017-02-17 15:37:15
JavaScript
CMTV, 2017-02-17 15:37:15

VK API. How to get the id of the "Like" widget when liking?

Hello!
I've been struggling with the following problem for a few days now, which I can't seem to solve.
There is a page with an arbitrary number of VK "Like" widgets. For example, like this:

<div id="widget_1"></div>
<script>VK.Widgets.Like("widget_1", {type: "vertical", pageURL: 'url_1'}, 1);</script>

<div id="widget_2"></div>
<script>VK.Widgets.Like("widget_2", {type: "vertical", pageURL: 'url_2'}, 2);</script>

VKontakte provides the VK Observer tool for catching likes.
You can do it like this:
VK.Observer.subscribe("widgets.like.liked", function f(likesNum)
{
alert ("Вы лайкнули! Количество лайков: " + likesNum);
});

As you can see, the number of widget likes is passed to the interceptor function. And that's all.
How can I find out the parameters (pageURL, page_id) of the widget that was used to like it?
===
This could be done by tracking the click on the widget along with "liked". But it is impossible to catch a click on the iframe of the widget in a normal way, and the existing crutches work every other time + ignore finger touches from smartphones.
===
UPD:
I tried to accept two parameters from VK Observer: a and b.
a contains the number of likes for the widget. expected.
But bcontains some number. Through experimentation, I realized that this number means the number of the widget on the page (not its id, just what number it was created on the current page).
This can be used to identify a widget, but is an undocumented feature. How long will she work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CMTV, 2017-02-18
@CMTV

Despite the fact that the VKontakte request was answered in the negative:

Hello Peter!
Sorry for the long wait.
Unfortunately, there is no way to implement your idea at the moment. The developers promised to make the observer accept the page_id parameter.
VK team.

But I found a way. At the moment - 02/18/2017 - VK Observer returns not one (as indicated in the documentation), but two parameters during the like event: likes_num and vk_widget_num.
VK.Observer.subscribe("widgets.like.liked", function f(likes_num,vk_widget_num) {
       /* ... */
    });

The first parameter likes_num contains the number of likes for the current widget. Everything is as it should be.
The second parameter, vk_widget_num, contains the number of already loaded widgets on the page (the "Like" widget, "Comments", "This is interesting" widget, etc.). In other words, VK gives you the second parameter which number (starting from 1) this widget will be on the page.
===== Solution to the problem
First, let's add an additional attribute to the HTML tags:
<div id="widget_1" class="like_widget" data-widget-number="1"></div>
<script>VK.Widgets.Like("widget_1", {type: "vertical", pageURL: 'url_1'}, 1);</script>

<div id="widget_1" class="like_widget" data-widget-number="2"></div>
<script>VK.Widgets.Like("widget_2", {type: "vertical", pageURL: 'url_2'}, 2);</script>

Now we register our "widgets.like.liked" event handler:
VK.Observer.subscribe("widgets.like.liked", function f(likes_num,vk_widget_num)
{
    var widget_id = $('.like_widget[data-widget-number=' + vk_widget_num + ']').attr('id');

    alert ("Вы лайкнули! Был нажат виджет с ID: " + widget_id + ". Количество лайков: " + likes_num);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question