[SalesForce] Collect tracking code implementation

I've been trying to get the Collect Tracking Code to work, unfortunately without success. I have inserted the code provided in the documentation and replaced MID, but I am not getting any results. So I contacted support, but they said they can't help because this is a custom development. They sent me this instruction, according to which I need to replace "INSERT_EMAIL_OR_UNIQUE_ID" with dynamic code to get the Subscriber ID. A Subscriber ID is typically an email address or number unique to that user. (https://help.salesforce.com/articleView?id=mc_ctc_set_user_info.htm&type=5 )

I have tried following but still no results:

`<script runat=server>
 var contactEmail = Platform.Variable.GetAttributeValue('EmailAddr')
</script>
<script type="text/javascript" src="http://12345678.collect.igodigital.com/collect.js">
</script>
<script type="text/javascript">
  _etmc.push(["setOrgId", "MID"]);
  _etmc.push(["setUserInfo", {"email": ' + contactEmail + '}]);     
  _etmc.push(["trackPageView"]);
</script>`

Can you plese let me know if above code is correct?
Do you have any examples of successful implementation of the Collect Tracking Code?

Thank you

Best Answer

Firstly, SSJS is only applicable if you are running the script on a CloudPage:

<script runat=server>
var contactEmail = Platform.Variable.GetAttributeValue('EmailAddr')
</script>

Also, you can't simply use variables across SSJS and "classic" front-end JS. How to fetch the subscriber key, I will get back to later.

Your next part is almost correct. I have removed http:, so the script uses same protocol as the page where it is embedded. I have replaced the MID with 12345678 - so you don't disclose the Business Unit where this is running - remember to replace it again with the correct one. You also didn't replace it in the setOrgId function call (it only stated MID in your code).

The following part is the same, regardless if you run the script on Cloud Page or anywhere else:

<script type="text/javascript" src="//12345678.collect.igodigital.com/collect.js"></script>

Next, we need to work with two examples. Firstly, if you run this on a CloudPage, you can fetch the Subscriber Key (I will NOT recommend you use email address or other PII in frontend) if your contact has been sent to the Cloud Page using CloudPagesUrl function:

<script type="text/javascript">
_etmc.push(["setOrgId", "12345678"]);
%%[
set @Id = AttributeValue("_subscriberkey")
IF NOT empty(@Id) then
OutputLine(Concat("_etmc.push([""setUserInfo"", {""email"": "",@Id,""}]);"))
ENDIF    
]%%
_etmc.push(["trackPageView"]);
</script>

If you run the script on a different page, you can fetch the id from the url (again - do use anything but email address here). E.g. from http://example.com?visitorid=11001100 you will be able to fetch the id using this script:

<script type="text/javascript">
  var urlParams = new URLSearchParams(window.location.search);
  var id = urlParams.get('visitorid');
  _etmc.push(["setOrgId", "12345678"]);
  if ( visitorid ) {
  _etmc.push(["setUserInfo", {"email": visitorid }]);
  }     
  _etmc.push(["trackPageView"]);
</script>
Related Topic