facebook Conversion API integration

Open to all! Ask other shopowners for help.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

facebook Conversion API integration

Post 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!


Join The Code Co-op to get access to your library in the Code Co-op Forum
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: facebook Conversion API integration

Post 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
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: facebook Conversion API integration

Post 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.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: facebook Conversion API integration

Post 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
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: facebook Conversion API integration

Post by loop »

can you give me here the hint about the db hook?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: facebook Conversion API integration

Post 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.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: facebook Conversion API integration

Post by loop »

and what means "hooks_action" in the hook db?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: facebook Conversion API integration

Post by ecartz »

It's correct in the SQL that you posted.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: facebook Conversion API integration

Post 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?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: facebook Conversion API integration

Post 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).


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply