1

I need to set some cookies based on some CTA button links. I will later be checking for these cookies to determine what content to display on the entry with the cta on the next visit and other entries etc. Seems like I would need to set the cookie via javascript on click event and then direct them to the url they clicked, anyone know of a way to do this without javascript and have an event handler for the cta clicks?

3

One option would be to set a query string on your cta urls, have your template look for that, and change your content accordingly.

You can't set a cookie in Twig directly but there's a few plugins to help you out, like the handy LJ Cookies.

So on your CTA you might do something like:

<a href="/cta-link-to/?cta=button2" class="cta-button">Buy now!</a>

Then in the template it links to:

{% if craft.request.getParam('cta') is not empty and craft.request.getParam('cta') == "button2" %}
    {# set the cookie #}
    {% do craft.lj_cookies.set('ctabuttonclicked', 'button2') %}
{% endif %}

To check for the cookie later on:

{% if craft.lj_cookies.get( 'ctabuttonclicked' ) == 'button2' %}
 .. do something ...
{% endif %}

As a quick aside, depending on how you want to track things, you may want to throw in a quick redirect to dump the query string off the URL before you spit out the subsequent page. Something like:

{% if craft.request.getParam('cta') is not empty and craft.request.getParam('cta') == "button2" %}
    {# set the cookie #}
    {% do craft.lj_cookies.set('ctabuttonclicked', 'button2') %}

    {# redirect to the same page but dump the query string#}
    {%redirect entry.getUrl() %}
{% endif %}

But it really depends on your campaign and how you want that content shared and consumed.

(Just a quick example: if someone hits your CTA from an e-mail you sent and really likes what they see and posts that link with the special query string to all their friends on Facebook, everyone is going to get that event cookie set and those deals, for better or worse. It could also piss people off if you don't have it... "What? I don't see those deals you sent me..." But that's a can of worms for another time...)

1
  • Thanks, I ended up just setting via javascript, and then checking via {% if getCookie('cookiename') %} but I think this would probably be better I will try this and test thanks. – Bowenac Jun 16 '15 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.