Page 1 of 2

facebook Conversion API integration

Posted: Sun Dec 29, 2024 11:33 am
by loop
hi All
i'm on a modul / hook for the facebook conversion API. in basic i need to make server curl request on different states, add to cart, successpage, view_products..

what is the best way to do this in a single place without editing to listen to "add_to_cart" and then make a serverside request and on checkout_success do it also...

is the best place a hook? what are the listening for add to cart? would really appriciate a hint / help
thanks!

Re: facebook Conversion API integration

Posted: Sun Dec 29, 2024 1:46 pm
by loop
i have made a hook ( i believe this is the way):

Code: Select all

<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/

include 'includes/classes/facebookConversionsApi.php';

class hook_shop_siteWide_facebookCapi {
    public function listen_injectSiteStart() {

        //pageview
        $fb_api = new FacebookConversionsAPI();
        $fb_api->sendEvent('ViewContent');

    }
}
but my first problem is, that the class facebookConversionsApi is not loaded automaticly and i have to include it myself in the hook (don't know if that's a good idea?)

if you have any tips / better idea i would appriciate...i will try now to extend this hook for every event (Succes page, add to cart, view product....)

and the last thing i cannot get to work is i need a hook for add_to_cart (like the action module)

namespace Phoenix\Actions;

class add_product {

public static function execute() {

i want that as soon as add_product is done i can fire the event

Re: facebook Conversion API integration

Posted: Sun Dec 29, 2024 2:52 pm
by ecartz
loop wrote: Sun Dec 29, 2024 1:46 pm the class facebookConversionsApi is not loaded automaticly and i have to include it myself in the hook (don't know if that's a good idea?)
Try renaming the file to facebook_conversions_a_p_i.php and see if it can find that.

If you are using the same variable multiple times, try

Code: Select all

$fb_api =& Guarantor::ensure_global('FacebookConversionsAPI');
instead of calling new. Or put that into a constructor.

Code: Select all

protected $fb_api;

public function __construct() {
  $this->fb_api = new FacebookConversionsAPI();
}
and use it like

Code: Select all

        $this->fb_api->sendEvent('ViewContent');
You might consider using database hooks for this rather than file hooks, as it might be easier to have database hooks share the same hook class for all your events. Or use ensure_global to use the same global variable every time.

Listening to actions, like Add to Cart would need to be done in a system hook, as they happen before siteWide is started. https://github.com/CE-PhoenixCart/Phoen ... .sql#L1185

You need to listen for the event before that. E.g. hook_shop_system__16fb_actions (if you are using file hooks with a global variable). You might want to check that product_id is set before generating the event. You definitely should check $_GET['action'] before reporting any of those events.

Re: facebook Conversion API integration

Posted: Sun Dec 29, 2024 3:13 pm
by loop
hi ecartz
thank you!
most of your comment i already done, but that with listening to add_cart i don't know exactly how you mean this:

do i need to move my filebased hook to system? class hook_shop_siteWide_facebookCapi ? can i have my code still in the same file or do i need to have 1 hook in siteWide and one in system?

i'm not really familiar with db hooks, should i make 2 hooks like:

Code: Select all

INSERT INTO hooks (hooks_site, hooks_group, hooks_action, hooks_code, hooks_class, hooks_method) VALUES ('shop', 'system', 'startApplication', '_16fb_actions', 'facebookCapi', 'add_to_cart');
INSERT INTO hooks (hooks_site, hooks_group, hooks_action, hooks_code, hooks_class, hooks_method) VALUES ('shop', 'siteWide', 'SiteStart', '_16fb_actions', 'facebookCapi', 'SiteStart');
i'm not sure about hooks_action what this is exactly and what i have to set here

and thann delete my hook in sitewide and make a class facebookCapi.php ?

Code: Select all

class facebookCapi {
    protected $fb_api;

    public function __construct() {
        $this->fb_api = new FacebookConversions_a_p_i();
    }

    public function add_to_cart() {
        mail("myemail@email.ch", "add_product0", "yes");

    }
    
    public function SiteStart() {
if possible and easier i like to switch to db hooks, but im not verry familiar with the database hooks and i don't know how to do it, thank you very much for your help

Re: facebook Conversion API integration

Posted: Mon Dec 30, 2024 10:15 pm
by loop
can you give me here the hint about the db hook?

Re: facebook Conversion API integration

Posted: Mon Dec 30, 2024 10:33 pm
by ecartz
loop wrote: Sun Dec 29, 2024 3:13 pm should i make 2 hooks like:
delete my hook in sitewide and make a class facebookCapi.php ?
That looks fine.

Re: facebook Conversion API integration

Posted: Mon Dec 30, 2024 10:40 pm
by loop
and what means "hooks_action" in the hook db?

Re: facebook Conversion API integration

Posted: Tue Dec 31, 2024 1:47 pm
by ecartz
It's correct in the SQL that you posted.

Re: facebook Conversion API integration

Posted: Thu Jan 02, 2025 8:03 pm
by loop
hmm but something doesn't work
i have the 2 hooks inserted

Code: Select all

INSERT INTO hooks (hooks_site, hooks_group, hooks_action, hooks_code, hooks_class, hooks_method) VALUES ('shop', 'system', 'startApplication', '_16fb_actions', 'facebookCapi', 'add_to_cart');
INSERT INTO hooks (hooks_site, hooks_group, hooks_action, hooks_code, hooks_class, hooks_method) VALUES ('shop', 'siteWide', 'SiteStart', '_16fb_actions', 'facebookCapi', 'SiteStart');
and i have the class: includes/classes/facebookCapi.php

Code: Select all

class facebookCapi {
    protected $fb_api;

    public function __construct() {
        $this->fb_api = new FacebookConversions_a_p_i();
    }

    public function add_to_cart() {
        mail("xx@xx.ch", "add_product0", "yes");
        echo "add_product0";
    }
    
    public function SiteStart() {
        echo "SiteStart"; .....
but nothing happends i don't receive a email or see a echo whenn i browse the test shop..so it seems something is wrong...any ideas?

Re: facebook Conversion API integration

Posted: Thu Jan 02, 2025 11:02 pm
by ecartz
Perhaps change the action from SiteStart to siteWideStart

Check that you are adding to cart (what happens on the product info page) and not buying now (from product listings).