diff --git a/src/FacebookPixel/FacebookPixel.php b/src/FacebookPixel/FacebookPixel.php index 7dddb46..e37a549 100755 --- a/src/FacebookPixel/FacebookPixel.php +++ b/src/FacebookPixel/FacebookPixel.php @@ -82,7 +82,7 @@ public function addToCart( ) { $this->sendEventToOutput( - $this->template->event = self::EVENT_ADD_TO_CART, + self::EVENT_ADD_TO_CART, $this->prepareProductsToParameters($contentIds, $contentName, $contentCategory, $value, $currency) ); } @@ -90,7 +90,7 @@ public function addToCart( public function purchase($value, $currency, $contentIds = null) { $this->sendEventToOutput( - $this->template->event = self::EVENT_PURCHASE, + self::EVENT_PURCHASE, $this->prepareProductsToParameters($contentIds, null, null, $value, $currency) ); } diff --git a/src/FacebookPixel/templates/facebookPixel.latte b/src/FacebookPixel/templates/facebookPixel.latte index 8eb7cf4..81c5341 100755 --- a/src/FacebookPixel/templates/facebookPixel.latte +++ b/src/FacebookPixel/templates/facebookPixel.latte @@ -1,21 +1,17 @@ - {if count($specialEvents)} - + {/if} diff --git a/tests/.gitignore b/tests/.gitignore index e7d2149..8b94733 100755 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,8 +1,9 @@ vendor composer.lock temp -!temp/.gitignore +!temp/cache/.htaccess log +!log/.htaccess tests/_temp tests/_log tests/_helpers \ No newline at end of file diff --git a/tests/app/presenters/HomepagePresenter.php b/tests/app/presenters/HomepagePresenter.php index 10485ad..816cf88 100644 --- a/tests/app/presenters/HomepagePresenter.php +++ b/tests/app/presenters/HomepagePresenter.php @@ -2,6 +2,7 @@ namespace App\Presenters; +use Eflyax\FacebookPixel\FacebookPixel; use Eflyax\FacebookPixel\FacebookPixelService; use Eflyax\FacebookPixel\IFacebookPixelFactory; use Nette\Application\UI\Presenter; @@ -17,41 +18,82 @@ class HomepagePresenter extends Presenter /** @var FacebookPixelService @inject */ public $facebookPixelService; + /** @var FacebookPixel */ + public $facebookPixel; + private $product; + private $currencyCode = 'CZK'; + protected function startup() { parent::startup(); + $this->facebookPixel = $this['facebookPixel']; $product = new \stdClass(); $product->id = 1; $product->price = 42; $product->title = 'Product title'; $product->description = 'Product description'; - $this->product = $product; } public function actionProductDetail() { - $this->template->product = $this->product; - $this->template->currencyCode = 'CZK'; + $this->facebookPixel->viewContent( + $this->product->id, + $this->product->title, + null, + $this->product->price, + $this->currencyCode + ); } public function actionPurchase() { - $this->template->totalPrice = $this->product->price + self::SHIPPING_PRICE; - $this->template->currencyCode = 'CZK'; - $this->template->productIds = [$this->product->id]; - $this->facebookPixelService->eventStart(FacebookPixelService::EVENT_PURCHASE); + $totalPrice = $this->product->price + self::SHIPPING_PRICE; + $this->facebookPixel->purchase($totalPrice, $this->currencyCode); } public function actionAddToCart() { // ..add product to shopping cart - $this->facebookPixelService->eventStart(FacebookPixelService::EVENT_ADD_TO_CART); + $this->facebookPixel->addToCart( + $this->product->id, + $this->product->title, + null, + $this->product->price, + $this->currencyCode + ); $this->redirect('ProductDetail'); } + public function actionAddToCartAndPurchase() + { + $totalPrice = $this->product->price + self::SHIPPING_PRICE; + $this->facebookPixel->addToCart( + $this->product->id, + $this->product->title, + null, + $this->product->price, + $this->currencyCode + ); + $this->facebookPixel->purchase($totalPrice, $this->currencyCode); + } + + public function actionAddToCartAndPurchaseWithRedirect() + { + $totalPrice = $this->product->price + self::SHIPPING_PRICE; + $this->facebookPixel->addToCart( + $this->product->id, + $this->product->title, + null, + $this->product->price, + $this->currencyCode + ); + $this->facebookPixel->purchase($totalPrice, $this->currencyCode); + $this->redirect('Homepage:'); + } + protected function createComponentFacebookPixel() { return $this->IFacebookPixelFactory->create(); diff --git a/tests/app/templates/@layout.latte b/tests/app/templates/@layout.latte index 8961386..7715ff4 100755 --- a/tests/app/templates/@layout.latte +++ b/tests/app/templates/@layout.latte @@ -1,17 +1,18 @@ - + + -{control facebookPixel} -
- {block content} + {block content}{/block}
+{control facebookPixel} + \ No newline at end of file diff --git a/tests/app/templates/Homepage/addToCartAndPurchase.latte b/tests/app/templates/Homepage/addToCartAndPurchase.latte new file mode 100755 index 0000000..e3484e6 --- /dev/null +++ b/tests/app/templates/Homepage/addToCartAndPurchase.latte @@ -0,0 +1 @@ +{block content} diff --git a/tests/app/templates/Homepage/productDetail.latte b/tests/app/templates/Homepage/productDetail.latte index b1b2ad8..58d34f8 100755 --- a/tests/app/templates/Homepage/productDetail.latte +++ b/tests/app/templates/Homepage/productDetail.latte @@ -1,19 +1,3 @@ {block content} -{control facebookPixel:viewContent, - $product->id, - $product->title, - $product->description, - $product->price, - $currencyCode -} - -{control facebookPixel:addToCart, - $product->id, - $product->title, - $product->description, - $product->price, - $currencyCode -} - Add to cart diff --git a/tests/app/templates/Homepage/purchase.latte b/tests/app/templates/Homepage/purchase.latte index 08b8664..e3484e6 100755 --- a/tests/app/templates/Homepage/purchase.latte +++ b/tests/app/templates/Homepage/purchase.latte @@ -1,7 +1 @@ {block content} - -{control facebookPixel:purchase, - $totalPrice, - $currencyCode, - $productIds -} \ No newline at end of file diff --git a/tests/tests/functional/src/EventTest.php b/tests/tests/functional/src/EventTest.php index a9cfb4e..54d7014 100644 --- a/tests/tests/functional/src/EventTest.php +++ b/tests/tests/functional/src/EventTest.php @@ -8,8 +8,8 @@ class EventTest extends BasePresenterTest public function testPageView() { $this->checkUrlAndResponse($this->generateLink('Homepage:')); - $this->tester->see('fbq(\'init\', ' . self::FB_PIXEL_ID . ')'); - $this->tester->see('fbq(\'track\', \'PageView\')'); + $this->tester->see("'init', '" . self::FB_PIXEL_ID . "'"); + $this->tester->see("'track', 'PageView'"); $this->tester->seeElement('img', [ 'src' => 'https://www.facebook.com/tr?id=' . self::FB_PIXEL_ID . '&ev=PageView&noscript=1' ]); @@ -18,10 +18,10 @@ public function testPageView() public function testViewContent() { $this->checkUrlAndResponse($this->generateLink('Homepage:productDetail')); - $this->tester->see('\'track\', "ViewContent"'); + $this->tester->see("'track', 'ViewContent'"); $this->checkProduct(); - // we didn't call start event AddToCart before - $this->tester->dontSee('\'track\', "AddToCart"'); +// // we didn't call start event AddToCart before + $this->tester->dontSee("'track', 'AddToCart'"); } public function testAddToCart() @@ -30,24 +30,35 @@ public function testAddToCart() $this->generateLink('Homepage:addToCart'), $this->generateLink('Homepage:productDetail') ); - $this->tester->see('\'track\', "AddToCart"'); + $this->tester->see("'track', 'AddToCart'"); $this->checkProduct(); } public function testPurchase() { $this->checkUrlAndResponse($this->generateLink('Homepage:purchase')); - $this->tester->see('\'track\', "Purchase"'); + $this->tester->see("'track', 'Purchase'"); + } + + public function testMultipleEvents() + { + $this->checkUrlAndResponse($this->generateLink('Homepage:addToCartAndPurchase')); + } + + public function testMultipleEventsWithRedirect() + { + $this->checkUrlAndResponse( + $this->generateLink('Homepage:addToCartAndPurchaseWithRedirect'), + $this->generateLink('Homepage:') + ); } private function checkProduct() { - $this->tester->see('"content_type":"product"'); - $this->tester->see('"content_ids":["1"]'); - $this->tester->see('"content_name":"Product title"'); - $this->tester->see('"content_category":"Product description"'); - $this->tester->see('"value":"42.00"'); - $this->tester->see('"currency":"\'CZK\'"'); + $this->tester->see("'content_type':'product'"); + $this->tester->see("'content_ids':['1']"); + $this->tester->see("'content_name':'Product title'"); + $this->tester->see("'currency':'CZK'"); } }