DevCEDTeam / CED

0 stars 0 forks source link

Description #21

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

Certainly! Here's the rewritten code that includes the Google tag:

const axios = require('axios');

const APIKEY = '3595aee955020c957ea6b304681d65be4e7b1772b1ecf24a355d189b838c4e7f2dca333d'; // Replace with your AC API Key
const APIURL = 'https://cfored.api-us1.com'; // Replace with your Active Campaign API URL
const EVENTKEY = '4f4bb543ca5991bbb0dd79808ba376efcf4f51e5'; // Replace with your ActiveCampaign Event Key
const ACTID = '224274263'; // Replace with your ActiveCampaign ID

const googleTag = `
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-TBRJ1817X7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-TBRJ1817X7');
</script>
`;

exports.activeCampaignProxy = async (req, res) => {
  const action = req.query.action;
  const hash = req.query.hash;
  const contactId = req.query.contactId;
  const email = req.query.email;
  const eventName = req.query.eventName;
  const eventData = req.query.eventData;

  if (action === 'trackEvent' && email) {
    try {
      await sendEvent(email, eventName, eventData);
      res.send(`${googleTag}\nEvent tracked successfully`);
    } catch (error) {
      res.status(400).end(error);
    }
  } else if (action === 'trackEvent' && contactId) {
    try {
      const email = await getEmail(contactId);
      await sendEvent(email, eventName, eventData);
      res.send(`${googleTag}\nEvent tracked successfully`);
    } catch (error) {
      res.status(400).end(error);
    }
  } else if (action === 'trackEvent' && hash) {
    try {
      const email = await getEmailHash(hash);
      await sendEvent(email, eventName, eventData);
      res.send(`${googleTag}\nEvent tracked successfully`);
    } catch (error) {
      res.status(400).end(error);
    }
  } else {
    res.status(400).end('Invalid action');
  }
};

async function sendEvent(email, eventName, eventData) {
  const url = 'https://trackcmp.net/event';
  const data = `actid=${ACTID}&key=${EVENTKEY}&event=${eventName}&eventdata=${eventData}&visit=%7B%22email%22%3A%22${encodeURIComponent(email)}%22%7D`;

  try {
    await axios.post(url, data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
  } catch (error) {
    throw error;
  }
}

async function getEmail(contactId) {
  const url = `${APIURL}/api/3/contacts/${contactId}`;

  try {
    const response = await axios.get(url, { headers: { 'Api-Token': APIKEY } });
    return response.data.contact.email;
  } catch (error) {
    throw error;
  }
}

async function getEmailHash(hash) {
  const url = `${APIURL}/admin/api.php?api_action=contact_view_hash&api_key=${APIKEY}&hash=${hash}&api_output=json`;

  try {
    const response = await axios.get(url, { headers: { 'Content

-Type': 'application/x-www-form-urlencoded' } });
    return response.data.email;
  } catch (error) {
    throw error;
  }
}

In this updated code, I've added the Google tag script to the googleTag variable, and it will be included in the response when the event is successfully tracked. The Google tag will be displayed before the "Event tracked successfully" message.