top of page

Integrate Facebook Conversions API in Wix, Using Velo





Who is our customer and what problem did they want to solve?


Our customer had previously connected their Facebook Pixel to Wix using the Wix Facebook Pixel & Conversions API Marketing Integration.


This had the clients' website tracking events they had set up in Facebook Business Manager with the pixel, but not with the conversions API.


We were tasked with implementing the Conversions API for as many of the events as we could (View Content, Add to Cart, and Initiate Checkout).



What solution did we provide?


Since the Conversions API is all about sending tracking data from the server rather than the client, we knew that we would need a way to send the event information from the frontend to the backend.


So we decided to make use of Wix HTTP Functions to provide an endpoint for the website to send event tracking requests to have them processed on the backend and sent to Facebook via the Conversions API.



How did we implement the solution?


We implemented a single endpoint called `trackEvent` to accomplish this, using the wix-fetch module as well as the wix-secrets-backend module to store & retrieve the Pixel Id and Access Token needed to successfully call the Conversions API. When called from the frontend this endpoint would take the data sent to it and format it to send to Facebook.


After this endpoint was implemented, we add code to the `masterPage.js` file which is included on every page of a Wix site.


The code parses information about what page the user is currently on and gathers data based on the event and then sends a request to the `trackEvent` endpoint to have it sent over to Facebook's Conversion API.


Check out the code we used below!


import { getSecret } from "wix-secrets-backend";
import { fetch } from "wix-fetch";

import { ok, badRequest } from "wix-http-functions";

const baseURL = "https://graph.facebook.com/v12.0/<PIXEL_ID>/events";

export async function post_trackEvent(request) {
  const pixelId = await getSecret("FbPixelId");
  const accessToken = await getSecret("FbCAPIAccessToken");

  const URL =
    baseURL.replace("<PIXEL_ID>", pixelId) + `?access_token=${accessToken}`;

  const today = new Date();
  const current_timestamp = Math.floor(today / 1000);

  const ipAddress = request.ip;
  const body = await request.body.json().then((json) => json);

  const userData = {
    client_ip_address: ipAddress,
    client_user_agent: body.userAgent,
  };

  let customData;

  if (body.content) {
    customData = {
      content_category: "All Products",
      content_name: body.content.name,
      value: body.content.value,
      quantity: body.content.quantity,
      content_type: "product",
      currency: "usd",
      content_ids: [body.content.id],
    };
  }

  if (body.eventName === "InitiateCheckout") {
    customData = {
      content_category: "All Products",
      contents: body.content.contents,
      value: body.content.value,
      content_type: "product",
      currency: "usd",
      content_ids: body.content.content_ids,
      num_items: body.content.num_items,
    };
  }

  const reqBody = {
    event_name: body.eventName,
    event_time: current_timestamp,
    event_source_url: body.sourceUrl,
    action_source: "website",
    user_data: userData,
  };

  if (customData) {
    reqBody.custom_data = customData;
  }

  const headers = {
    "Content-Type": "application/json",
  };

  fetch(URL, {
    method: "POST",
    headers,
    body: JSON.stringify({ data: [reqBody] }),
  })
    .then((res) => res.json())
    .then((json) => {
      console.log(json);
    })
    .catch((err) => {
      console.log("Error: ", err);
    });

  const response = {
    headers: {
      "Content-Type": "application/json",
    },
  };

  try {
    response.body = {
      response: {
        pixelId,
      },
    };

    return ok(response);
  } catch (err) {
    response.body = {
      error: err,
    };

    return badRequest(response);
  }
}


Have any questions? Contact us at 225-250-1888 or email robert@roberthebertmedia.com.



About our company


RHM specializes in helping businesses of all sizes and across all industries achieve their digital and web marketing needs. Whether it's designing a new website, building an app, performing custom development, or running Google Ads, our goal is to showcase how you are the best at what you do and help people connect with you. Contact us at 225-250-1888 to get started!

 
 
 

96 Comments


markmiller29927
5 hours ago

Dạo gần đây mình có gặp tên lv88 vài lần nên hôm nay mới thử mở ra xem. Mình chủ yếu xem những phần hiện ra trước rồi chuyển sang vài mục khác để hiểu tổng thể. Có chỗ khá dễ theo dõi, cũng có phần mình phải đọc lại mới hiểu được ý chính. Vì chưa sử dụng thực tế nên những gì mình biết hiện tại chỉ dựa trên việc lướt và đọc thông tin. Mình nghĩ một lần xem như vậy cũng đủ để có khái niệm ban đầu, còn muốn hiểu kỹ hơn chắc phải dành thêm thời gian.

Like

amandayoung42471
5 hours ago

Mình tình cờ biết tới lc88 khi đang xem một cuộc trao đổi trên mạng. Ban đầu mình chỉ định mở ra xem nhanh, nhưng sau đó cũng lướt thêm vài phần để hiểu trang này tập trung vào những nội dung nào. Có khá nhiều mục khác nhau nên lần đầu xem mình chủ yếu đọc những phần đơn giản trước. Một số thông tin mình vẫn chưa tìm hiểu kỹ nên chưa rõ hoàn toàn. Dù vậy, sau lần truy cập này mình ít nhất cũng đã biết thêm về cái tên mà trước đó chỉ thấy thoáng qua.

Like

Có lần mình đang xem một số nội dung trên mạng thì thấy nhắc tới ao88, nhưng lúc đó chỉ lướt qua. Sau đó mình mới quay lại mở thử và xem kỹ hơn vài mục. Mình không có ý định tìm hiểu tất cả trong một lần nên chỉ đọc những phần khiến mình chú ý trước. Một số nội dung khá dễ nắm bắt, còn vài chỗ mình vẫn chưa hiểu hết bối cảnh. Vì mới dừng ở mức xem thông tin nên hiện tại mình chưa muốn đưa ra nhận xét quá cụ thể.

Like

williamthomas49577
15 hours ago

Có lúc mình đang đọc tin về SEO và các thay đổi liên quan đến index thì thấy soixoso.net xuất hiện trong danh sách mình đang xem. Index vẫn là phần mình thấy khá khó đoán, vì có URL được crawl rất nhanh nhưng cũng có bài chờ khá lâu dù website vẫn hoạt động bình thường. Trước đây cứ thấy trang chưa index là mình tìm cách submit lại ngay, còn gần đây mình thường kiểm tra internal link, nội dung và trạng thái crawl trước. Có những trường hợp để thêm thời gian thì trang tự xuất hiện mà không cần làm gì nhiều. Vì thế mình đang cố phân biệt vấn đề kỹ thuật thực sự với những…

Like

donnaking15383
17 hours ago

Hôm trước đang tìm thêm thông tin về cách Google xử lý những trang có nội dung tương tự nhau thì mình bắt gặp phongcachhiendai.net. Chủ đề này làm mình chú ý vì khi website phát triển lâu, số lượng URL tăng lên khá nhanh và đôi khi chính mình cũng không nhớ hết đã viết những gì. Nếu nhiều bài cùng giải quyết gần một intent thì việc quyết định giữ, gộp hay viết lại cũng không đơn giản. Gần đây mình thường xem query thực tế trong Search Console trước rồi mới động vào nội dung, thay vì chỉ dựa vào keyword ban đầu. Cách này giúp nhìn rõ hơn Google đang hiểu từng URL theo hướng nào.…

Like
bottom of page