Is is safe to use javascript with tokens in snippets?

Anonymous
Not applicable

Is is safe to use javascript with tokens in snippets?

I want to add segment.io code to several thank you pages that look like this. I also plan to replace the values with tokens? Is this doable?

analytics.identify('019mr8mf4r', {
    name             : 'Achilles',
    email            : 'achilles@segment.io',
    subscriptionPlan : 'Premium',
    friendCount      : 29
});
Tags (1)
1 REPLY 1
Anonymous
Not applicable

Re: Is is safe to use javascript with tokens in snippets?

Hi Patrick:

The snippet editor is rich text, which filters our abnormal HTML. This means any PHP, JavaScript, or CSS. The way you CAN use a snippet is to put a text token in its place that contains the JavaScript. This is only worthwhile if you're planning to make the JavaScript dynamic by segments. If you're just looking for an easy way to one-click change the tracking code, I'd recommend just a pure text token on all those thank-you pages, which contains all the JavaScript.

Option for doing this if you're hard-coding the script in your template:

analytics.identify('{{my.segmentioID:default=019mr8mf4r}}', {
    name             : '{{my.segmentioName:default=Achilles}}',
    email            : '{{my.segmentioEmail:default=achilles@segment.io}}',
    subscriptionPlan : '{{my.segmentioSubscriptionPlan:default=Premium}}',
    friendCount      : {{my.segmentioFriendCount:default=29}}
});


Or

{{my.segmentioJavaScript}} which just refers to the entire tracking code.

I would also stick with text tokens, and avoid rich text tokens in this case. With a rich text token, the values will be filtered through and possibly changed, especially with JavaScript or PHP. Even though this means one line and less pretty code, it's worth sticking with text tokens for inserting code.

If you're trying to change the tracking code with dynamic snippets, I would recommend you place the JavaScript in tokens. Because snippets are rich text, your code will be filtered out and broken if you try to use snippets without text tokens.

So you could have:

Segment 1: {{my.segmentioTrackingCode1}}
Segment 2: {{my.segmentioTrackingCode2}}

Snippets/nonsnippets aren't the problematic part, the rich text fields are where there's danger with abnormal HTML.

Bonus points:

JavaScript thrown into a random element in your page is poor development practice. It should go in the <head> or at the end of the <body>. I like to place a few extra empty tokens in the <head> and in the appropriate spot in the end of the <body> for uses like this. That way you can decide later that you want some JS/CSS and put it in the proper place without having to change every single template and reapprove every single landing page. 

I like:

{{my.universalCSS1}}
{{my.universalCSS2}}
{{my.universalHeadJS}}
{{my.universalBodyJS}}

Then you can also define a top-level campaign folder with the tokens and override them on individual programs or lower-level campaign folders later on.


Best,
Edward Unthank
Marketing Operations Specialist
Yesler