How to associate email address with site visitor using custom form

I have seen several things similar, but not exactly on point. Let’s say we have ActiveCampaign “site tracking” enabled. A visitor comes to the site and browses around. At some point they enter their email address in an ActiveCampaign form. At that point, their tracking history, and any UTM data in the URL during their initial referral to the site will be connected to this new Contact that was created when they entered their email address, correct?

OK, now let’s say we have a custom form/survey on the site that is too complicated to create using the ActiveCampaign form builder. But in the process of taking that survey, we ask a visitor for their email. What I want to do is the same as the first scenario, that is, link up the site tracking history and any referral data with this new contact. So: (1) I can use the REST API to make an Ajax call at the point we get the email address, and then server-side check if there is an existing contact and if not, create a new contact. All good there. But … (2) If it’s a new contact, I then want to link the web tracking from this visitor to that new contact I just created server-side. Can I then directly access the ActiveCampaign JavaScript object (I see this referred to as ‘vgo’ or ‘pgo’??) to link those? I have seen this: vgo(‘setEmail’, ‘someemail@email.com’) in another thread. But I am not clear that this can be called once we are on a page, or does it have to get injected into the script as it is loaded on the page – so I would have to stash the email and make sure we loaded a new page at some point? The later is not ideal. I’d rather just be able to call a js function and go on with the survey, since that is all happening via Ajax without a page reload. Thanks for any help!

Hi! Apologies for taking so long to get an answer for you!

I assume you’ve already done this, but the first step is to have the site tracking code set up on your site and your domain is whitelisted in your ActiveCampaign account. The site tracking code will contain something like the following.

  vgo('setAccount', '########'); 
  vgo('setTrackByDefault', true);
  vgo('process'); 

The vgo object is needed to integrate your custom form with ActiveCampaign’s site tracking. As part of your form’s submission process, you will need to use the vgo object to associate the current contact and reload the site tracking script. Here’s an example using jQuery.

jQuery('form#####').submit(function() {
  // your form submission code
  // get the email address of the contact created/updated
  var userEmail = jQuery("form##### input[type='email']").val();
  // set the tracking email and update tracking
  vgo('setEmail', userEmail);
  vgo('process');
});

I hope this helps!