top of page

Integrate Facebook Conversions API in Wix, Using Velo

Jun 28, 2022
3 min read




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!

 
 
 

406 Comments


Đoạn hướng dẫn kết nối dữ liệu chuyển đổi bằng Velo có khá nhiều chi tiết kỹ thuật, nhưng cách chia từng phần khiến tôi vẫn theo được ý chính. Chuyện gửi dữ liệu từ phía máy chủ làm tôi nhớ lần nghe người quen bàn về việc đo lường trên các trang game bài đổi thưởng. Trong cuộc nói chuyện ấy, họ nhắc đến nhà cái uy tín, còn tôi chỉ chú ý đến câu hỏi dữ liệu nào thực sự cần được lưu. Tôi không biết cái tên đó có dùng phương thức tương tự hay không. Sau khi đọc bài, điều tôi quan tâm nhất vẫn là ranh giới giữa đo lường chính xác và thu thập…

Like

Đọc xong bài này tôi mới chú ý đến chi tiết nhỏ: tác giả không chỉ nói về form trên Wix, mà còn bàn việc sự kiện được gửi lại cho Facebook khi trình duyệt không còn là đường duy nhất. Đoạn dùng Velo làm tôi liên tưởng đến lúc ngồi chờ tin nhắn trễ, tay vẫn bấm mấy trò giải trí rảnh rỗi. Một người quen từng kể về mấy ứng dụng game bài đổi thưởng, rồi kể cả tên 789club, nhưng tôi chỉ nghe lướt qua. Quay lại bài, điều giữ tôi lại không phải tên công cụ, mà là cách một hành động nhỏ được ghi nhận. Nghĩ vậy mà tôi đọc chậm hơn ở phần cuối.

Like

richardlewis718
2 days ago

Đọc phần hướng dẫn tích hợp Facebook Conversions API trong Wix, tôi chú ý nhất đến chuyện dữ liệu cần được truyền đúng và có mục đích rõ ràng. Lúc nghỉ giữa chừng, tôi nhớ từng nghe một người quen nhắc đến game bài đổi thưởng và cái tên kèo nhà cái 5, nhưng đó chỉ là một mẩu chuyện thoáng qua, không đủ để tôi hiểu cụ thể. Vì vậy tôi thấy bài này hữu ích ở chỗ nó tập trung vào quy trình và bối cảnh kỹ thuật, thay vì nói chung chung về công cụ. Tôi vẫn cần đọc chậm lại để hiểu các bước liên quan đến Velo.

Like

Phần tích hợp dữ liệu chuyển đổi trong Wix làm tôi nhận ra một thao tác tưởng nhỏ ở giao diện có thể đi qua khá nhiều lớp kỹ thuật phía sau. Khi đọc đến chuyện dữ liệu được gửi và đối chiếu, tôi liên tưởng tới những lần thấy mọi người bàn về cách các trang game bài đổi thưởng ghi nhận hoạt động; kèo nhà cái từng xuất hiện trong một cuộc nói chuyện như vậy, nhưng tôi không rõ hệ thống của họ vận hành thế nào. Tôi quan tâm hơn đến bài viết ở góc độ kỹ thuật: luồng dữ liệu cần được mô tả rõ, kiểm tra cẩn thận và chú ý quyền riêng tư.

Like

Bài viết tập trung vào cách tích hợp Facebook Conversions API với Wix bằng Velo, nên phần đáng chú ý với tôi là vai trò của dữ liệu gửi từ máy chủ bên cạnh dữ liệu trên trình duyệt. Tôi vừa đọc xong và vẫn muốn hiểu rõ hơn những trường hợp nào cần tự viết mã thay vì dùng bongdalu cách tích hợp có sẵn. Bài có nhắc tới các bước kỹ thuật, nhưng với người mới dùng Velo thì đoạn nối giữa cấu hình và việc kiểm tra dữ liệu có thể cần giải thích thêm. Nhìn chung, đây là một chủ đề khá cụ thể, không chỉ nói chung về theo dõi chuyển đổi.

Like
bottom of page