Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit dbea0b0

Browse files
committed
Initial Commit
1 parent 69b68d2 commit dbea0b0

12 files changed

+341
-2
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
# Craft Storage (cache) [http://buildwithcraft.com/help/craft-storage-gitignore]
21
/craft/storage/*
3-
!/craft/storage/logo/*
2+
!/craft/storage/logo/*
3+
*.swp
4+
node_modules
5+
.DS_Store

FbPixelPlugin.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace Craft;
3+
4+
class FbPixelPlugin extends BasePlugin
5+
{
6+
public function init()
7+
{
8+
craft()->fbPixel->listen();
9+
}
10+
11+
public function getName()
12+
{
13+
return Craft::t('Facebook Pixel');
14+
}
15+
16+
public function getDescription()
17+
{
18+
return Craft::t('Integrates Facebook Pixel with Craft Commerce');
19+
}
20+
21+
public function getVersion()
22+
{
23+
return '0.0.1';
24+
}
25+
26+
public function getDeveloper()
27+
{
28+
return 'Moment, Inc';
29+
}
30+
31+
public function getDeveloperUrl()
32+
{
33+
return 'http://github.com/Moment-Inc';
34+
}
35+
}

resources/icon.svg

+1
Loading

services/FbPixelService.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixelService extends BaseApplicationComponent
6+
{
7+
/*
8+
* This is called in MomentPlugin::init(). It's mainly responsible for
9+
* what happens on a page load.
10+
*
11+
*/
12+
public function listen()
13+
{
14+
if (getenv('FB_PIXEL_NOOP')) {
15+
return;
16+
}
17+
18+
craft()->templates->hook('fbPixel.renderBase', [
19+
$this, 'renderBase'
20+
]);
21+
22+
craft()->fbPixel_viewContent->listen();
23+
craft()->fbPixel_initiateCheckout->listen();
24+
craft()->fbPixel_purchase->listen();
25+
craft()->fbPixel_addToCart->listen();
26+
}
27+
28+
/*
29+
* This render the moment/templates/fbpixel/base.twig which contains the fb pixel code
30+
*
31+
*/
32+
public function renderBase()
33+
{
34+
return $this->renderTemplate('base', [
35+
'fbPixelId' => getenv('FB_PIXEL_ID')
36+
]);
37+
}
38+
39+
public function renderEvent($eventName, $eventData)
40+
{
41+
return $this->renderTemplate('event', [
42+
'eventName' => $eventName,
43+
'eventData' => $eventData
44+
]);
45+
}
46+
47+
public function renderTemplate($template, $templateData = [])
48+
{
49+
# this is required because we're not using CP routes for this service
50+
$oldPath = craft()->path->getTemplatesPath();
51+
$templatePath = craft()->path->getPluginsPath() . 'fbpixel/templates/';
52+
craft()->path->setTemplatesPath($templatePath);
53+
$template = craft()->templates->render($template, $templateData);
54+
craft()->path->setTemplatesPath($oldPath);
55+
56+
return $template;
57+
}
58+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixel_AddPaymentInfoService extends BaseApplicationComponent
6+
{
7+
public function render()
8+
{
9+
$cart = craft()->commerce_cart->getCart();
10+
11+
$eventData = [
12+
'value' => $cart->totalPrice,
13+
'currency' => 'USD',
14+
'content_ids' => array_map(function($i) { return $i->sku; }, $cart->lineItems),
15+
'content_category' => 'Checkout',
16+
];
17+
18+
return craft()->fbPixel->renderTemplate('addPaymentInfo', ['eventData' => $eventData]);
19+
}
20+
}

services/FbPixel_AddToCartService.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixel_AddToCartService extends BaseApplicationComponent
6+
{
7+
const FLASH_NAME = '_fbPixelVariantIds';
8+
9+
private $variantIds;
10+
11+
public function listen()
12+
{
13+
craft()->on('multiAdd_cart.MultiAddToCart', [
14+
craft()->fbPixel_addToCart, 'addFlash'
15+
]);
16+
17+
craft()->fbPixel_addToCart->checkFlash();
18+
}
19+
20+
public function checkFlash()
21+
{
22+
if (craft()->userSession->hasFlash(self::FLASH_NAME)) {
23+
$this->variantIds = craft()->userSession->getFlash(self::FLASH_NAME, null, true);
24+
$this->addHook();
25+
}
26+
}
27+
28+
public function addHook()
29+
{
30+
craft()->templates->hook('fbPixel.renderBase', [
31+
$this, 'renderTemplate'
32+
]);
33+
}
34+
35+
public function addFlash($event)
36+
{
37+
$lineItems = $event->params['lineItems'];
38+
39+
$variantIds = craft()->userSession->getFlash(self::FLASH_NAME);
40+
41+
if (empty($variantIds)) {
42+
$variantIds = [];
43+
}
44+
45+
$variantIds = array_merge(
46+
$variantIds,
47+
$this->getVariantIds($event->params['lineItems'])
48+
);
49+
50+
craft()->userSession->setFlash(self::FLASH_NAME, $variantIds);
51+
}
52+
53+
public function renderTemplate()
54+
{
55+
$template = '';
56+
57+
foreach ($this->variantIds as $variantId) {
58+
$variant = craft()->commerce_variants->getVariantById($variantId);
59+
60+
$eventData = [
61+
'value' => $variant->salePrice,
62+
'currency' => 'USD',
63+
'content_name' => 'Add To Cart',
64+
'content_ids' => $variant->sku,
65+
'content_type' => 'product'
66+
];
67+
68+
$template .= craft()->fbPixel->renderEvent('AddToCart', $eventData);
69+
}
70+
71+
return $template;
72+
}
73+
74+
private function getVariantIds($lineItems)
75+
{
76+
return array_map( function($lineItem) {
77+
$purchasable = $lineItem->purchasable;
78+
79+
if (!empty($purchasable->defaultVariant)) {
80+
return $purchasable->defaultVariant->id;
81+
} else {
82+
return $purchasable->id;
83+
}
84+
85+
}, $lineItems);
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixel_InitiateCheckoutService extends BaseApplicationComponent
6+
{
7+
public function listen()
8+
{
9+
craft()->templates->hook('fbPixel.renderInitiateCheckout', [
10+
craft()->fbPixel_initiateCheckout, 'render'
11+
]);
12+
}
13+
14+
public function render()
15+
{
16+
$cart = craft()->commerce_cart->getCart();
17+
18+
$addPaymentInfoRaw = craft()->fbPixel_addPaymentInfo->render();
19+
20+
$item = [
21+
'content_name' => 'Checkout',
22+
'content_ids' => array_map(function($i) { return $i->sku; }, $cart->lineItems),
23+
'content_type' => 'product',
24+
'num_items' => $cart->getTotalQty(),
25+
'value' => $cart->totalPrice,
26+
'currency' => 'USD'
27+
];
28+
29+
30+
return $addPaymentInfoRaw . craft()->fbPixel->renderEvent('InitiateCheckout', $item);
31+
}
32+
}

services/FbPixel_PurchaseService.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixel_PurchaseService extends BaseApplicationComponent
6+
{
7+
const FLASH_NAME = '_fbPixelOrderId';
8+
9+
private $order;
10+
11+
public function listen()
12+
{
13+
craft()->on('commerce_orders.onOrderComplete', [
14+
craft()->fbPixel_purchase, 'addFlash'
15+
]);
16+
17+
craft()->fbPixel_purchase->checkFlash();
18+
}
19+
20+
public function checkFlash()
21+
{
22+
if (craft()->userSession->hasFlash(self::FLASH_NAME)) {
23+
$orderId = craft()->userSession->getFlash(self::FLASH_NAME, null, true);
24+
$order = craft()->commerce_orders->getOrderById($orderId);
25+
$this->order = $order;
26+
$this->addHook();
27+
}
28+
}
29+
30+
public function addHook()
31+
{
32+
craft()->templates->hook('fbPixel.renderPurchase', [
33+
$this, 'renderTemplate'
34+
]);
35+
}
36+
37+
public function addFlash($event)
38+
{
39+
craft()->userSession->setFlash(self::FLASH_NAME, $event->params['order']->id);
40+
}
41+
42+
public function renderTemplate()
43+
{
44+
$eventData = [
45+
'content_name' => 'Purchase',
46+
'content_ids' => array_map(function($i) { return $i->sku; }, $this->order->lineItems),
47+
'content_type' => 'product',
48+
'num_items' => $this->order->getTotalQty(),
49+
'value' => $this->order->totalPrice,
50+
'currency' => 'USD'
51+
];
52+
53+
return craft()->fbPixel->renderEvent('Purchase', $eventData);
54+
}
55+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Craft;
4+
5+
class FbPixel_ViewContentService extends BaseApplicationComponent
6+
{
7+
public function listen()
8+
{
9+
craft()->templates->hook('fbPixel.renderViewContent', [
10+
$this, 'render'
11+
]);
12+
}
13+
14+
public function render(&$context)
15+
{
16+
return craft()->fbPixel->renderEvent('ViewContent', $context['fbPixelItem']);
17+
}
18+
}

templates/addPaymentInfo.twig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- Facebook Pixel AddPaymentInfo Code -->
2+
<script>
3+
;(function (window) {
4+
window.fbPixelAddPaymentInfoSendEvent = function () {
5+
fbq('track', 'AddPaymentInfo', JSON.parse('{{ eventData|json_encode|e("js") }}'));
6+
}
7+
})(window);
8+
</script>
9+
<!-- End Facebook Pixel AddPaymentInfo Code -->

templates/base.twig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Facebook Pixel Code -->
2+
<script>
3+
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
4+
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
5+
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
6+
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
7+
document,'script','https://connect.facebook.net/en_US/fbevents.js');
8+
9+
fbq('init', '{{ fbPixelId }}');
10+
fbq('track', 'PageView');
11+
</script>
12+
13+
<noscript><img height="1" width="1" style="display:none"
14+
src="https://www.facebook.com/tr?id={{ fbPixelId }}&ev=PageView&noscript=1"
15+
/></noscript>
16+
17+
<!-- End Facebook Pixel Code -->

templates/event.twig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- Facebook Pixel {{ eventName }} Code -->
2+
<script>
3+
fbq('track', '{{ eventName }}', JSON.parse('{{ eventData|json_encode|e("js") }}'));
4+
</script>
5+
<!-- End Facebook Pixel {{ eventName }} Code -->

0 commit comments

Comments
 (0)