Laura Hilhorst

Using JavaScript in hyperlinks in mailings

Written by Laura Hilhorst on

From now on, hyperlinks in Copernica mailings can be provided with a “data-script” attribute. With this interesting new feature, it's possible to link follow-up actions to hyperlinks in an alternative, powerful way.

Normally, JavaScript code gets filtered out of mailings by programs that show received messages. Therefore, it's of no use to provide hyperlinks in mailings with, for example, an “onclick” or “onhover” attribute to add scripting functionality. With the data-script tag, however, you can run dynamic JavaScript code when someone clicks a link.

An example

If you want to edit a profile based on a click on a hyperlink, you could do so by using the following hyperlink:

<a href="" data-script="profile.fields.preference = 'sport'">

The data-script tag is a Copernica-specific HTML-extension. Upon sending the email, this attribute is deleted (to make sure it doesn't reach the recipient). Because Copernica alters all URLs in mailings to register clicks, everyone who clicks on a link first reaches Copernica's servers. There, the original hyperlink is restored to quickly redirect the user, and the attribute is added again.

In the attribute's JavaScript code, it's possible to add all sorts of instructions for reading profile data or editing profiles. The script below, for example, increments the profile field “amountofclicks” by one each time the link is clicked.

<a href="" data-script="profile.fields.amountofclicks++">

Available data

Right now, we're working on rolling out the first version of this system. This means that the amount of available features is still limited. At this moment, you can only use the data-script attribute to read and edit profile data. To do this, use the variables “profile” and “subprofile”.

A profile has an “id”-property (read-only) and a “fields”-property (read+write) to be able to reach all profile fields. For convenience, you may omit “fields”:

profile.fields.preference = 'football';
profile.preference = 'football';

Both statements above have the same result. However, it's better to include “fields” anyway, just in case we assign a different meaning to “profile.preference” in the future - which is unlikely, but still something to consider.

The “old” follow-up system

Naturally, the old system will still be functional. This new system is being developed as an expansion of the current system. We'll be adding new features in the future, such as scheduling follow-up emails.