Facebook Pixel Firing Twice for Paypal

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290368

-- HikaShop version -- : 3.2.2
-- Joomla version -- : 3.8.5

I've added my Facebook Tracking pixel to the top of the "after_end" view. As you get redirected to Paypal, everything is fine. Normally, you would expect the pixel to fire once payment has been confirmed in paypal and after the redirection back to the thank you page (after_end view file). However, if you cancel the paypal payment and return to the payment window on my site, the pixel fires....even though the "after_end" file has not been called.

I've checked multiple times and the pixel is only on the "end" (for first data payment) and "after_end" (for PayPal). Any reason why canceling the PayPal payment would result in the "after_end" view file being called? Then, in the case where users canceled it and decided to pay with First Data, i see an instance where the pixel was fired twice for the same purchase. Obviously, this is showing false positives in my tracking statistics.

I should note that it's working properly for all confirmed orders, and seems to only be a problem on orders where customers cancel the payment and return to our website. The only reason I noticed this behavior is the Facebook conversion stats show I sold more than what I actually sold. The difference seems to be the canceled payment amount.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #290377

Hi,

The after_end view file is only called after the paypal payment. It cannot be called before. I don't see how it would.
I presume that it must be the pixel code you've added in the "end" view file which is fired when it shouldn't because of the way you added it to it.

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290437

I added the script to the beginning of the end file before the opening <?php. Could this be the problem? Or more likely, is this due to me using an older version of the "end" and "after_end" display views?

<!-- Facebook Event snippet conversion page -->
<script>
  fbq('track', 'Purchase', <?php
if(!defined('DS'))
 define('DS', DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true;

$app =JFactory::getApplication();
$order_id = $app->getUserState('com_hikashop.order_id');
$orderClass = hikashop_get('class.order');
$order = $orderClass->get($order_id);
$currencyClass = hikashop_get('class.currency');
$currency_id = intval($order->order_currency_id);
$currency = $currencyClass->getCurrencies($currency_id, $null);
echo "{value: ".$order->order_full_price.", currency:'".$currency[$currency_id]->currency_code."'}";
?>);
</script>
<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.6.1
 * @author	hikashop.com
 * @copyright	(C) 2010-2016 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>


<?php
if(empty($this->html)){
	echo '<div class="span6">';
	echo JText::_('THANK_YOU_FOR_PURCHASE');
	echo '</div>';
}else{
	echo '<div class="span6">';
	echo $this->html;
	echo '</div>';
}

$this->nextButton = false;

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290445

Fixed the pixel firing by moving the script withing <?php if(empty($this->html)){ XXXX } ?>

The only other question i have is how to prevent the pixel from firing again if you reload the thank you page???

Last edit: 6 years 7 months ago by mojooutdoors-holden.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #290472

Hi,

Indeed, the way you had the code placed there meant that the pixel was also added for each paypal transaction since the paypal payment method also uses the "end" view before the redirection to add its form to it through the $this->html variable.
Placing it in the if is indeed a good solution.
To avoid your code running several times if the customer reload the page, you should set a variable in the user session and check against it.
Something like that:

if(!isset($_SESSION['pixel_displayed'])) {
  $_SESSION['pixel_displayed'] = true;
 // your code
}

Note also that since the view files of HikaShop are in HikaShop, HikaShop is already loaded.
So the code
if(!defined('DS'))
 define('DS', DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true;
is not necessary in the view files of HikaShop or in the plugins of the group "hikashop", "hikashoppayment" or "hikashopshipping".

Note also that the code:
$app =JFactory::getApplication();
$order_id = $app->getUserState('com_hikashop.order_id');
$orderClass = hikashop_get('class.order');
$order = $orderClass->get($order_id);
should also not be necessary as the order data is already available in $this->order in the "end" and the "after_end" view files.

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290506

That worked perfectly! No issues whatsoever now. The session handler really helped out as well!

Awesome support as always!

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290507

One last question pertaining to the session handler. Is there any way to set the session to expire after, let's say, 5 minutes? I've never used this before, and after a quick Google search, it seemed like there were ways but it either depending on your php.ini file or there was a way to declare it inside the if statement to check the amount of time that had passed before the session variable was initialized. However, most claimed the latter didn't always work.

The reason I ask is that I tried two test orders 10 minutes apart and the second one didn't fire the pixel.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #290512

Hi,

Sure. You can do like that for example:

if(!isset($_SESSION['last_order_time'])) {
 // init of the timer
 $_SESSION['last_order_time'] = time();
}else{
 $time_since_last_order = time() - $_SESSION['last_order_time'];
 if( $time_since_last_order > 600) { 
   // more than 10 minutes
  }else{
  // less than 10 minutes
  }
}
As you can see it's just basic PHP.

Note that in Joomla, you don't need, and shouldn't create or destroy the PHP session. That's handled by Joomla automatically for you. You just want to set a timer in it to know how much time has passed since last order.

The following user(s) said Thank You: mojooutdoors-holden

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #290982

Works perfectly! Thanks again for the awesome support!

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291332

I have one more issue. Now that the pixel is firing for the Paypal orders, it's not firing for the credit card orders.

I believe the view file that displays after the credit card payment is processed is "checkout/end.php"

Currently I have the following:

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	3.2.2
 * @author	hikashop.com
 * @copyright	(C) 2010-2018 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
if(empty($this->html)) {
	echo JText::_('THANK_YOU_FOR_PURCHASE');
	$user = JFactory::getUser();
	if(!$user->guest) {
		$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id); ?>		
		<!-- Facebook Event snippet for Turkey Sales conversion page -->
        <script>
          fbq('track', 'Purchase', <?php echo "{value: ".$this->order->order_full_price.", currency:'USD'}"; ?>);
        </script>
<?php 
	} else {
		$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id.'&order_token='.$this->order->order_token); ?>
		<!-- Facebook Event snippet for Turkey Sales conversion page -->
        <script>
          fbq('track', 'Purchase', <?php echo "{value: ".$this->order->order_full_price.", currency:'USD'}"; ?>);
        </script>
<?php
	}
	echo '<br/>'.JText::sprintf('YOU_CAN_NOW_ACCESS_YOUR_ORDER_HERE', $url);
} else {
	echo $this->html;
}
$this->nextButton = false;

Am i missing something obvious???

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #291365

Hi,

You're basing yourself on whether $this->html is empty or not to display the facebook pixel.
However, it's possible that some credit card payment plugins (which display the credit card form directly during the checkout) fill the $this->html variable with their own thank you page instead of HikaShop's default one.

So instead of basing yourself on that variable, it would be better to check whether $this->order->order_payment_method == 'paypal'

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291388

I see. So would it be better to use the following on both the end and after_end view files?

<php if ($this->order->order_payment_method == 'paypal'  &&  $this-> order->order_status == 'confirmed') { 

<!-- FB PIXEL -->

} elseif ($this->order->order_payment_method == 'firstdata'  &&  $this-> order->order_status == 'confirmed'){

<!-- FB PIXEL -->

} else {

<!-- DON'T USE PIXEL -->

} ?>

Also be better to put this as the head of the file or within the " if(empty($this->html)) " conditional???

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #291409

Hi,

You can even simplify your code and have:

<php if ($this-> order->order_status == 'confirmed') { 

<!-- FB PIXEL -->

} else {

<!-- DON'T USE PIXEL -->

} ?>
since the order_status won't be confirmed for PayPal on the "end" view file and you customers won't be directed to "after_end" for firstdata.

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291445

Now last question. Should this go at the very beginning like this:

<php if ($this-> order->order_status == 'confirmed') { 

<!-- FB PIXEL -->

} else {

<!-- DON'T USE PIXEL -->

} ?>


<?php
/**
 * @package	HikaShop for Joomla!
 * @version	3.2.2
 * @author	hikashop.com
 * @copyright	(C) 2010-2018 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
$app = JFactory::getApplication();
$app->enqueueMessage( JText::_('THANK_YOU_FOR_PURCHASE') );
$user = JFactory::getUser();
if(!$user->guest){
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id); ?>
<?php 

}else{
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id.'&order_token='.$this->order->order_token); ?>
<?php 
}
$app->enqueueMessage(JText::sprintf('YOU_CAN_NOW_ACCESS_YOUR_ORDER_HERE',$url));

or within the if statement further down like this:
<?php
/**
 * @package	HikaShop for Joomla!
 * @version	3.2.2
 * @author	hikashop.com
 * @copyright	(C) 2010-2018 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
$app = JFactory::getApplication();
$app->enqueueMessage( JText::_('THANK_YOU_FOR_PURCHASE') );
$user = JFactory::getUser();
if(!$user->guest){
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id); ?>
<?php 

<php if ($this-> order->order_status == 'confirmed') { 

<!-- FB PIXEL -->

} else {

<!-- DON'T USE PIXEL -->

} ?>


}else{
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id.'&order_token='.$this->order->order_token); ?>

<php if ($this-> order->order_status == 'confirmed') { 

<!-- FB PIXEL -->

} else {

<!-- DON'T USE PIXEL -->

} ?>

<?php 
}
$app->enqueueMessage(JText::sprintf('YOU_CAN_NOW_ACCESS_YOUR_ORDER_HERE',$url));



**** ALSO, i noticed there isn't a closing php tag at the end of the default "after_end" file? Was this on purpose?

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #291453

Hi,

You should put it at the beginning yes.
Ideally, it's best to put it just after the code:

defined('_JEXEC') or die('Restricted access');
?>
because that code is a security feature of Joomla and help prevent against hacking attempts.

No closing PHP tag at the end is done on purpose.
If you happen to have extra characters after the last closing tag of a PHP file, these characters will end up in the output buffer of PHP. It might happen inadvertedly. And if you try to ouput headers after characters have been already ouput, it could generate errors and event break the downloads of files.
So it's better to never have closing PHP tags at the end of PHP files to avoid such risks. It's a good practice:
stackoverflow.com/questions/3219383/why-...-the-closing-php-tag

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291490

Thanks for the info. It works for first data, but it did not work for PayPal.

Therefore, I added it to the top of the "after_end" file. Seems to be firing properly for Paypal orders. Any reason why this would not work?

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	3.2.2
 * @author	hikashop.com
 * @copyright	(C) 2010-2018 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>

<?php if ($this-> order->order_status == 'confirmed') { ?>
<!-- FB PIXEL -->
<script>
  fbq('track', 'Purchase', <?php
      echo "{value: ".$this->order->order_full_price.", currency:'USD'}";
      ?>);
</script>
<?php } else { ?>
<!-- DON'T USE PIXEL -->
<?php } ?>

<?php
$app = JFactory::getApplication();
$app->enqueueMessage( JText::_('THANK_YOU_FOR_PURCHASE') );
$user = JFactory::getUser();
if(!$user->guest){
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id);
}else{
	$url = hikashop_completeLink('order&task=show&cid='.$this->order->order_id.'&order_token='.$this->order->order_token);
}
$app->enqueueMessage(JText::sprintf('YOU_CAN_NOW_ACCESS_YOUR_ORDER_HERE',$url));

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #291512

Hi,

That's normal. You need to add the code to both end and after_end. If you only add it to end, it's normal that it would work for PayPal due to the way the system works.

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291559

I think there may be some confusion. I added this to both files, but I was pointing out that if you add the pixel code to "after_end" after the following code

defined('_JEXEC') or die('Restricted access');
?>
the pixel doesn't fire properly. I was forced to add it at the very beginning of the file for it to work properly.

However, i'm able to add it after this block in the "end" file.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 7 months ago #291561

It should be the same. It's just a security check. Note that if you put it before, you of course have to put it before the <?php tag or the code will break or be commented out based on where you place it exactly.

The following user(s) said Thank You: mojooutdoors-holden

Please Log in or Create an account to join the conversation.

  • Posts: 303
  • Thank you received: 18
6 years 7 months ago #291784

I agree. Still didn't work when added after that security check. For now, i just added it at the beginning of the file with proper php opening and closing tags.

Thanks again!

Please Log in or Create an account to join the conversation.

Time to create page: 0.081 seconds
Powered by Kunena Forum