Skip to content

Commit 3c35a79

Browse files
authored
Added server-side PageView event (#750)
1 parent 4beb072 commit 3c35a79

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

app/code/Meta/Conversion/Block/Pixel/Head.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,14 @@ public function getCustomer()
133133

134134
return $this->customerSession->getCustomer();
135135
}
136+
137+
/**
138+
* Get Event name
139+
*
140+
* @return string
141+
*/
142+
public function getEventToObserveName()
143+
{
144+
return 'facebook_businessextension_ssapi_page_view';
145+
}
136146
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Conversion\Model\Tracker;
22+
23+
use Meta\Conversion\Api\TrackerInterface;
24+
25+
class PageView implements TrackerInterface
26+
{
27+
private const EVENT_TYPE = 'PageView';
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function getEventType(): string
33+
{
34+
return self::EVENT_TYPE;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function getPayload(array $params): array
41+
{
42+
return [];
43+
}
44+
}

app/code/Meta/Conversion/etc/frontend/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<type name="Meta\Conversion\Controller\Pixel\Tracker">
55
<arguments>
66
<argument name="pixelEvents" xsi:type="array">
7+
<item name="facebook_businessextension_ssapi_page_view" xsi:type="object">Meta\Conversion\Model\Tracker\PageView</item>
78
<item name="facebook_businessextension_ssapi_view_category" xsi:type="object">Meta\Conversion\Model\Tracker\ViewCategory</item>
89
<item name="facebook_businessextension_ssapi_view_content" xsi:type="object">Meta\Conversion\Model\Tracker\ViewContent</item>
910
<item name="facebook_businessextension_ssapi_search" xsi:type="object">Meta\Conversion\Model\Tracker\Search</item>

app/code/Meta/Conversion/view/frontend/templates/pixel/head.phtml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
<?php /** @var \Meta\Conversion\Block\Pixel\Head $block */ ?>
1+
<?php
2+
3+
use Magento\Framework\Escaper;
4+
use Meta\Conversion\Block\Pixel\Head;
5+
6+
/**
7+
* @var Head $block
8+
* @var Escaper $escaper
9+
*/
10+
11+
$trackerUrl = $block->getTrackerUrl();
12+
?>
213
<?php /** @var \Magento\Framework\Escaper $escaper */ ?>
314
<?php if ($block->getFacebookPixelID()) { ?>
415
<!-- Meta Business Extension for Magento 2 -->
@@ -21,10 +32,27 @@ fbq(
2132
<?= /* @noEscape */ $block->getPixelInitCode() ?>,
2233
{agent: '<?= /* @noEscape */ $block->getFacebookAgentVersion() ?>' }
2334
);
24-
fbq('track', 'PageView', {
25-
source: "<?= /* @noEscape */ $block->getSource() ?>",
26-
pluginVersion: "<?= /* @noEscape */ $block->getPluginVersion() ?>"
27-
});
35+
</script>
36+
<script type="text/x-magento-init">
37+
{
38+
"*": {
39+
"Meta_Conversion/js/metaPixelTracker" : {
40+
"url" : "<?= $escaper->escapeUrl($trackerUrl); ?>",
41+
"payload": <?= /* @noEscape */ json_encode([
42+
"eventName" => $block->getEventToObserveName(),
43+
]) ?>,
44+
"browserEventData": <?= /* @noEscape */ json_encode([
45+
'fbAgentVersion' => $block->getFacebookAgentVersion(),
46+
'fbPixelId' => $block->getFacebookPixelID(),
47+
'source' => $block->getSource(),
48+
'pluginVersion' => $block->getPluginVersion(),
49+
'track' => 'track',
50+
'event' => 'PageView',
51+
'payload' => []
52+
]) ?>
53+
}
54+
}
55+
}
2856
</script>
2957
<!-- End Meta Pixel Code -->
3058
<?php } else { ?>

0 commit comments

Comments
 (0)