0

I'm currently setting up the FB Pixel for the first time (next.js). In development mode everything is working as expected and correctly.

My problem is production mode: I found out that the user ip address is somehow different in browser and server. When I hardcode an ip address, the tracking and deduplication works fine. But as soon as I get the value from server side props (header "x-forwarded-for"), the events from browser and server are tracked (event id is the same, user agent and ip are also passed) - but not deduplicated. By trying and playing with the code I found out that the ip address must be the issue.

Here is some relevant code:

//page.js
export async function getServerSideProps(context) {
  const { req } = context;
  const ip = req.headers["x-forwarded-for"];
  const ua = req.headers["user-agent"];
  return {
    props: {
      ip,
      ua,
    },
  };
}

//and then use the props like so:
  let values = {};
  values.ip = ip;
  useEffect(() => {
    fbTriggerViewContent(values);
  }, []);
  
  
//fbTriggerViewContent.js
const createSendingData = async (values, eventId) => {
  return {
    event_name: "ViewContent",
    event_id: eventId,
    event_time: Math.floor(Date.now() / 1000), 
    action_source: "website",
    event_source_url: window.location.href,
    user_data: {
      client_user_agent: values.ua,
      client_ip_address: "192.158.1.38"
        /* process && process.env.NODE_ENV === "development"
          ? "192.158.1.38"
          : values.ip, */
    },
  }; 
}; 

export default async function fbTriggerViewContent(values) {
  const additionalData = {};

  const eventId = crypto.randomUUID();

  const sendData = await createSendingData(values, eventId);

  fbq.event("ViewContent", additionalData, { eventID: eventId });

  axios({
    url: `https://graph.facebook.com/v20.0/${process.env.NEXT_PUBLIC_FB_PIXEL_ID}/events?access_token=${process.env.NEXT_PUBLIC_FB_ACCESS_TOKEN}`,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    data: {
      data: [sendData],
      test_event_code: process.env.NEXT_PUBLIC_TEST_ID,
    },
  }).then((response) => response);
}

Can anyone help me with this issue?

3
  • We don't know your setup, so we can't say how your system populates x-forwarded-for. What does req.connection.remoteAddress get you?
    – CBroe
    Commented Jul 4 at 8:57
  • Both are different. Additionally, I just realized, that the ip address is not send as 'user_data', when I get it from the server side props (ssp). When I hardcode an ip everything works fine in dev and production mode (events tracked and deduplicated). It seems as if the ip from the ssp is not send (but I can console.log the values in the createSendingData function). Btw, the user agent from the ssp is send and works fine.
    – Ewax_Du
    Commented Jul 4 at 20:28
  • Ok, so it seems as if the ip address is always send (I can see it in the payload in production and dev mode). BUT in production it is not displayed in the 'user data keys' (events manager -> Test events)... hope you know what I mean ;)
    – Ewax_Du
    Commented Jul 4 at 20:54

0