DevCEDTeam / CED

0 stars 0 forks source link

Step 2: Create a Cloud Function to generate tracking pixel #2

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

In this step, we'll create a Cloud Function that generates a tracking pixel image and assigns a random 12-digit alphanumeric code to each email address. To achieve this, we'll use Node.js and the canvas library to generate the pixel image and the crypto library to generate the random code. Here's the code for this step:

const { createCanvas, loadImage } = require('canvas');
const crypto = require('crypto');

exports.generateTrackingPixel = async (req, res) => {
  const codeLength = 12;
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let trackingCode = '';

  for (let i = 0; i < codeLength; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    trackingCode += characters.charAt(randomIndex);
  }

  const canvas = createCanvas(1, 1);
  const ctx = canvas.getContext('2d');

  // Set up any desired styling for the pixel
  ctx.fillStyle = '#000';
  ctx.fillRect(0, 0, 1, 1);

  const buffer = canvas.toBuffer('image/png');
  res.set('Content-Type', 'image/png');
  res.send(buffer);

  // Store the tracking code or use it to associate information with the email address
  console.log(`Generated tracking code: ${trackingCode}`);
};
DevCEDTeam commented 1 year ago

/

farhanmalik121 commented 1 year ago

The pixel image will contain the tracking code ??