{"id":16594,"date":"2017-08-17T13:49:58","date_gmt":"2017-08-17T13:49:58","guid":{"rendered":"https:\/\/www.mautic.org\/tracking-visitor-data-by-smart-url\/"},"modified":"2025-03-20T16:26:39","modified_gmt":"2025-03-20T16:26:39","slug":"tracking-visitor-data-by-smart-url","status":"publish","type":"post","link":"https:\/\/mautic.org\/blog\/tracking-visitor-data-by-smart-url","title":{"rendered":"Tracking Visitor Data by Smart URL"},"content":{"rendered":"\n<p><em>We&#8217;re excited to continue sharing content developed by the Mautic community.<\/em><\/p>\n\n\n\n<p>Have you ever wondered if you could track more than just visits via the Mautic tracking script? I know I did, and in this article I will show you how we can use URL\u2019s to track anything you want.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Basics<\/h4>\n\n\n\n<p>Before we start let\u2019s have a look at these two URL examples. This one is without extra parameter:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">https:\/\/mydomain.com\/page\/test\/\n<\/pre>\n\n\n\n<p>And here we have the same URL with an extra parameter:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">https:\/\/mydomain.com\/page\/test\/?email=johndoe@company.com\n<\/pre>\n\n\n\n<p>If you use for example MailChimp or CampaignMonitor to send out marketing emails, consider using this technique. Simply add the email address as a parameter at the end of the URL.<\/p>\n\n\n\n<p>In php one can catch this parameter simply with a:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$email = $_GET[\u2018email\u2019];\n<\/pre>\n\n\n\n<p>And then proceed to use that parameter for processing. However, there is also a clever way of using the Mautic Javascript to track visitors on your site. Only some slight modifications can make it send off any lead field you want to Mautic.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Get Tracking with Mautic<\/h4>\n\n\n\n<p>Here is the default script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script>\n    (function(w,d,t,u,n,a,m){w&amp;#91;'MauticTrackingObject']=n;\n        w&amp;#91;n]=w&amp;#91;n]||function(){(w&amp;#91;n].q=w&amp;#91;n].q||&amp;#91;]).push(arguments)},a=d.createElement(t),\n        m=d.getElementsByTagName(t)&amp;#91;0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)\n    })(window,document,'script','https:\/\/mydomain.com\/mtc.js','mt');\n\n    mt('send', 'pageview');\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>In short, what this does is simply pass along the identification parameters such as IP address and device fingerprint, together with the page the user is visiting. Now have a look at the script below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script>\n\tfunction getUrlParameter(name) {\n\t\tname = name.replace(\/&#91;&#91;]\/, '\\&#91;').replace(\/&#91;]]\/, '\\]');\n\t\tvar regex = new RegExp('&#91;\\?&amp;]' + name + '=(&#91;^&amp;#]*)');\n\t\tvar results = regex.exec(location.search);\n\t\treturn results === null ? '' : decodeURIComponent(results&#91;1].replace(\/+\/g, ' '));\n\t};\n\t\n    (function(w,d,t,u,n,a,m){w&#91;'MauticTrackingObject']=n;\n        w&#91;n]=w&#91;n]||function(){(w&#91;n].q=w&#91;n].q||&#91;]).push(arguments)},a=d.createElement(t),\n        m=d.getElementsByTagName(t)&#91;0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)\n    })(window,document,'script','https:\/\/domain.com\/mtc.js','mt');\n\n\tvar email  = getUrlParameter('e');\n\n\tif(email !== ''){\n\t\tmt('send', 'pageview', {email: email});\n\t} else {\n\t\tmt('send', 'pageview');\n\t}\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>The first function is used to extract a custom parameter from the URL. You can use this function for any parameter you want. Just make sure to pass the correct name to the function. In our case the name is \u2018email\u2019.<\/p>\n\n\n\n<p>Below that you can see the default part of the Mautic tracking script. This is not relevant for us. It does what it needs to do, no need to concern us with that for now. Below that you will see a new variable is created with the name \u2018email\u2019 and the value it will get is the URL parameter for \u2018email\u2019. This means that this variable will hold the content of the URL that comes after \u2018?email=\u2019, which should be the email address of the user.<\/p>\n\n\n\n<p>Then we have a short if-statement. This statement simply checks if the email parameter is filled or not. If it is not (which is the case for people that visit your website without a newsletter link) it will simply pass along the usual parameters. But if the email parameter is present it will pass along the usual parameters together with the email address.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sending a Custom Parameter<\/h4>\n\n\n\n<p>You can send this along by adding \u2018{email: email}\u2019 to the mt method. Keep in mind that the first string identifies the field you want to pass along. In this case we want to pass along the email, but this could also be any other field like firstname or lastname, or even a custom field you have created. The second string or value is the actual value that needs to be passed along. Since we put the value we got from the URL in the \u2018email\u2019 parameter we just write \u2018email\u2019 to indicated that we want to pass along the string inside this value.<\/p>\n\n\n\n<p>Lastly, make sure you enable public updating for the fields you want to track using this method. If public updating is not enabled Mautic will simply drop the field. You can do this by going to the \u2018Custom fields\u2019 section, then selecting the field you want and changing \u2018publicly updatable\u2019 from \u2018no\u2019 to \u2018yes\u2019.<\/p>\n\n\n\n<p>Congratulations! You can now track custom parameters on your website!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Possibilities Are Endless<\/h4>\n\n\n\n<p>Using the Mautic tracking script this way opens up new ways to identify your contacts. Think about the contact points you have and how you can use them to track different data sets.<\/p>\n\n\n\n<p>If you are using mailing tools like Mailchimp or CampaignMonitor it is super easy to add the add extra parameters to your newsletters. Simply append the extra parameter to all the URL\u2019s in your mail and there you go! In fact, you can use any contact network on point you have and use this methods for the parameters at hand.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Security<\/h4>\n\n\n\n<p>We should not forget that we are still handing personal data here. If you are appending sensitive data to a URL make sure you think about handling the data safely. What you could do is hash the parameters and de-hash them in your website before you send them off to Mautic. This way anyone who might come across the link cannot read or use the parameters.<\/p>\n\n\n\n<p>Also make sure to use https, also known as SSL if possible. This is simply an extra layer of security added to the request the user will make. Best practice is to use both SSL for your website as well as the connection between your website and Mautic. You will see in the examples above the I have used https in the code, and not http.<\/p>\n\n\n\n<p>And there you go. Now you know how to track custom parameters with the Mautic tracking script. I cannot wait to see what creative ways you all come up with to use this!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered if you could track more than just visits via the Mautic tracking script?<\/p>\n","protected":false},"author":52,"featured_media":16118,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"13","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"94198","discourse_permalink":"https:\/\/forum.mautic.org\/t\/tracking-visitor-data-by-smart-url\/35324","wpdc_publishing_response":"success","wpdc_publishing_error":"","footnotes":""},"categories":[1494,1495],"tags":[746,798,846,877,928,977,1009,1142,1190,1216,1251,1252,1262,1264],"class_list":["post-16594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-for-developers","category-for-integrators","tag-automation","tag-code","tag-developer","tag-email","tag-function","tag-javascript","tag-marketing","tag-parameter","tag-security","tag-ssl","tag-track","tag-tracking","tag-url","tag-users"],"acf":[],"_links":{"self":[{"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/posts\/16594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/comments?post=16594"}],"version-history":[{"count":2,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/posts\/16594\/revisions"}],"predecessor-version":[{"id":21750,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/posts\/16594\/revisions\/21750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/media\/16118"}],"wp:attachment":[{"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/media?parent=16594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/categories?post=16594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mautic.org\/wp-json\/wp\/v2\/tags?post=16594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}