drconsolidated
11/27/2019 - 7:55 PM

ActiveCampaign JS Event Tracking with GTM

ADAPTED from: https://www.activecampaign.com/blog/how-to-setup-activecampaign-event-tracking#events-to-ac-using-js

Note - There are a few places that you will need to change the code based on some of your ActiveCampaign account settings.

// your ActiveCampaign id. You can get this from your AC settings var actid = ""; // your event key, also in AC settings var eventKey = ""; // whatever event you want to track. var event = "Scrolled Halfway" // identify the user using the visit parameter var visit = { // the user's email address email: "" }

<!-- NOTE: to use, replace "_event_name_ xTEMPLATEx" above with a short identifier for this event, UPDATE the "event" value in the code below, delete this first line, AND update Triggering settings on this Tag to fire only when event should run -->

<script>
  // sets your ActiveCampaign id, from AC event settings
  var actid = "{{ActiveCampaign - Account ID}}";
  console.log('Acct ID = ', actid);
  // sets your event key, also in AC settings
  var eventKey = "{{ActiveCampaign - Event Key}}";
  console.log('eventKey = ', eventKey);
 
// UPDATE - DEFINE A NAME FOR THE EVENT YOU WANT TO TRACK - must match event name set in ActiveCampaign here: https://emp.activehosted.com/app/settings/tracking
  var event = "UPDATE_EVENT_NAME"
  
  // identifies user by email
  var visit = { email: '{{Email - From Cookie}}' }
  console.log('Visit = ', visit);
  // gets the url of the page and send it as event data
  var eventData = window.location.href;
  
    // fallback for CORS-blocking of request; added from comment fix at https://www.activecampaign.com/blog/how-to-setup-activecampaign-event-tracking#events-to-ac-using-js
  var trackcmp = document.createElement("script");
  trackcmp.async = true;
  trackcmp.type = 'text/javascript';
  
  // builds the eventString based on the variables you just edited above ☝
  var eventString = "actid=" + actid
                    + "&key=" + eventKey
                    + "&event=" + event
                    + "&visit=" + encodeURIComponent(JSON.stringify(visit))
                    + "&eventdata=" + {{Page URL}};
  
  // also part of fallback for CORS-blocking of request
  	trackcmp.src = '//trackcmp.net/event?' + eventString;
	var trackcmp_s = document.getElementsByTagName("script");
	var trackcmp_h = document.getElementsByTagName("head");
	trackcmp_h[0].appendChild(trackcmp);
    
  // sends the event to the ActiveCampaign API with our event values
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "https://trackcmp.net/event", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send(eventString);

</script>