Using AnalyticsJS with Qualified
October 29, 2024
The interactions that your team has with users within the Qualified messenger on your website can be used to trigger the AnalyticsJS Identify event. After inserting just a few lines of code, anytime a web visitor provides their email address into the Qualified messenger, the interaction will be used to deanonymize the user, and be recorded as an event that is viewable in your CaliberMind reports.
Getting Started
A pre-requisite is that you must already have the AnalyticsJS installed on your website to ensure that the analytics.identify call is processed correctly.
*Note: The AnalyticsJS script should be deployed before the Qualified function and main Qualified Javascript code in the website <head> section
Once the AnalyticsJS is on your website, find out where your Qualified JavaScript code is located on your company’s website. Some customers decide to place this directly into the site header, while others use Google Tag Manager.
Immediately after the qualified.js JavaScript code, place the following lines of code:
<script>
function qualifiedIdentifyEmail (d) {
var d = d;
for (const [key, value] of Object.entries(d)) {
if (typeof value === "string" && /.+\@.+\..+/.test(value)) {
return value;
}
}
};
qualified('handleEvents', function(name, data) {
switch(name) {
case 'Email Captured': {
if (data && data.field_values && qualifiedIdentifyEmail(data.field_values) != null) {
var eV = qualifiedIdentifyEmail(data.field_values);
analytics.identify(eV, { email: eV });
break;
}
else if (data && data.message && qualifiedIdentifyEmail(data.message) != null) {
var eV = qualifiedIdentifyEmail(data.message);
analytics.identify(eV, { email: eV });
break;
}
break;
}
default:
}
})
</script>
Once this has been added, your full lines of code should look something like this (with the “XXXXXXXXXXXX” being your Qualified account’s unique account-specific code):
// This is the Qualified function
<script>
(function(w,q){w['QualifiedObject']=q;w[q]=w[q]||function(){
(w[q].q=w[q].q||[]).push(arguments)};})(window,'qualified')
</script>
// This is the main Qualified JS script
<script async="" src="https://js.qualified.com/qualified.js?token=XXXXXXXXXXXX"></script>
// This is the new function to add immediately after the Qualified JS script
<script>
function qualifiedIdentifyEmail (d) {
var d = d;
for (const [key, value] of Object.entries(d)) {
if (typeof value === "string" && /.+\@.+\..+/.test(value)) {
return value;
}
}
};
qualified('handleEvents', function(name, data) {
switch(name) {
case 'Email Captured': {
if (data && data.field_values && qualifiedIdentifyEmail(data.field_values) != null) {
var eV = qualifiedIdentifyEmail(data.field_values);
analytics.identify(eV, { email: eV });
break;
}
else if (data && data.message && qualifiedIdentifyEmail(data.message) != null) {
var eV = qualifiedIdentifyEmail(data.message);
analytics.identify(eV, { email: eV });
break;
}
break;
}
default:
}
})
</script>
After the code has been placed onto your website, contact your Qualified Success Architect or issue a support ticket with the Qualified Technical Support team – requesting that they “enable event handling on your account.”
With this code added and event handling enabled, you will be able to trigger the AnalyticsJS “Identify” event any time that a website visitor enters their email address within your Qualified messenger.
*Note: Customers adding the code snippet to Google Tag Manager and receiving an ECMASCRIPT error message on deployment may use the script below instead:
<script>
function qualifiedIdentifyEmail (d) {
var d = d;
var keys = Object.keys(d);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = d[key];
if (typeof value === "string" && /.+\@.+\..+/.test(value)) {
return value;
}
}
};
qualified('handleEvents', function(name, data) {
if (name !== 'Email Captured' || !data || (!data.field_values && !data.message)) {
return;
}
else if (name === 'Email Captured' && data && data.field_values && qualifiedIdentifyEmail(data.field_values) !== null && qualifiedIdentifyEmail(data.field_values) !== undefined) {
var eV = qualifiedIdentifyEmail(data.field_values);
analytics.identify(eV, { email: eV });
}
else if (name === 'Email Captured' && data && data.message && qualifiedIdentifyEmail(data.message) !== null && qualifiedIdentifyEmail(data.message) !== undefined) {
var eV = qualifiedIdentifyEmail(data.message);
analytics.identify(eV, { email: eV });
}
});
</script>