Skip to content

Commit 09c71a0

Browse files
authored
Merge pull request #36 from webmatch/feature/productClickTracking
Feature Product Click Tracking
2 parents 5bd8588 + 30c2f14 commit 09c71a0

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [3.5.0]
2+
- Add product click tracking support
3+
14
## [3.4.0]
25
- add possibility to add script-tag-attributes
36
- add possibility to extend googletagmanager.com URL

Resources/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<tag name="shopware.event_subscriber" />
3232
</service>
3333

34+
<service id="wbm_tag_manager.subscriber.frontend.theme"
35+
class="WbmTagManager\Subscriber\Frontend\ThemeSubscriber">
36+
<tag name="shopware.event_subscriber"/>
37+
<argument>%wbm_tag_manager.plugin_dir%</argument>
38+
</service>
39+
3440
<service id="wbm_tag_manager.subscriber.frontend.dispatch" class="WbmTagManager\Subscriber\Frontend\Dispatch">
3541
<argument type="service" id="wbm_tag_manager.variables" />
3642
<argument type="service" id="config" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(function ($, window) {
2+
$.plugin('wbmProductClickTracking', {
3+
init: function () {
4+
var me = this;
5+
6+
me.setImpressions();
7+
8+
me._on(me.$el, 'click', $.proxy(me.onProductClicked, me));
9+
},
10+
/**
11+
* @param {jQuery.Event} event
12+
*/
13+
onProductClicked: function (event) {
14+
var me = this,
15+
ordernumber = $(event.target).closest('.product--box').data('ordernumber'),
16+
product = me.impressions.find(function (value, index) {
17+
return value.id === ordernumber;
18+
});
19+
20+
if (product === undefined) {
21+
return;
22+
}
23+
24+
window.dataLayer.push({
25+
'event': 'productClick',
26+
'ecommerce': {
27+
'click': {
28+
'actionField': {'list': product.list},
29+
'products': [ product ]
30+
}
31+
}
32+
});
33+
},
34+
setImpressions: function () {
35+
var me = this;
36+
37+
if (window.dataLayer) {
38+
for (i = 0; i < window.dataLayer.length; i++) {
39+
var layer = window.dataLayer[i];
40+
41+
if (layer.ecommerce && layer.ecommerce.impressions) {
42+
me.impressions = layer.ecommerce.impressions;
43+
}
44+
}
45+
}
46+
}
47+
});
48+
49+
$.subscribe('plugin/swInfiniteScrolling/onFetchNewPageFinished', function () {
50+
StateManager.addPlugin('.product--box a', 'wbmProductClickTracking');
51+
});
52+
53+
StateManager.addPlugin('.product--box a', 'wbmProductClickTracking');
54+
})(jQuery, window);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace WbmTagManager\Subscriber\Frontend;
3+
4+
use Doctrine\Common\Collections\ArrayCollection;
5+
use Enlight\Event\SubscriberInterface;
6+
7+
class ThemeSubscriber implements SubscriberInterface
8+
{
9+
/** @var string */
10+
private $pluginDir;
11+
12+
public function __construct(string $pluginDir)
13+
{
14+
$this->pluginDir = $pluginDir;
15+
}
16+
17+
public static function getSubscribedEvents(): array
18+
{
19+
return [
20+
'Theme_Compiler_Collect_Plugin_Javascript' => 'addJsFiles',
21+
];
22+
}
23+
24+
public function addJsFiles(): ArrayCollection
25+
{
26+
$jsFiles = [];
27+
$jsDir = $this->pluginDir . '/Resources/views/frontend/_public/src/js/';
28+
$jsFiles[] = $jsDir . 'jquery.product-click-tracking.js';
29+
30+
return new ArrayCollection($jsFiles);
31+
}
32+
}

plugin.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
<label lang="de">Tag Manager</label>
44
<label lang="en">Tag Manager</label>
55

6-
<version>3.4.0</version>
6+
<version>3.5.0</version>
77
<link>http://www.webmatch.de</link>
88
<author>Webmatch GmbH</author>
99
<compatibility minVersion="5.6.3" />
1010

11+
<changelog version="3.5.0">
12+
<changes lang="de">Hinzufügen der Möglichkeit des tracken von Produktklicks</changes>
13+
<changes lang="en">Add possibility to track product clicks</changes>
14+
</changelog>
15+
1116
<changelog version="3.4.0">
1217
<changes lang="de">Hinzufügen der Möglichkeit das Script Tag und die Tag Manager URL zu erweitern</changes>
1318
<changes lang="en">Add possibility to extend script tag and tag manager URL</changes>

0 commit comments

Comments
 (0)