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

<p>You can't set a cookie in Twig directly but there's a few plugins to help you out, like the handy <a href="https://github.com/lewisjenkins/craft-lj-cookies/" rel="nofollow">LJ Cookies</a>. </p>

<p>So on your CTA you might do something like:</p>

<pre><code>&lt;a href="/cta-link-to/?cta=button2" class="cta-button"&gt;Buy now!&lt;/a&gt;
</code></pre>

<p>Then in the template it links to:</p>

<pre><code>{% 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 %}
</code></pre>

<p>To check for the cookie later on:</p>

<pre><code>{% if craft.lj_cookies.get( 'ctabuttonclicked' ) == 'button2' %}
 .. do something ...
{% endif %}
</code></pre>

<p>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:</p>

<pre><code>{% 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 %}
</code></pre>

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

<p>(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...)</p>

CC BY-SA 3.0
2015-06-16T19:26:14.957Z
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
CC BY-SA 3.0
2015-06-16T18:56:44.707Z
2015-06-16T19:31:24.200Z
2015-06-16T19:31:24.200Z
RitterKnight
<p>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?</p>

<plugin-development><templating><cookies>
Setting cookies from CTA before directing to url?
# Setting cookies from CTA before directing to url?

(Asked by: username_0 on 2015-06-16)

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?

**Tags:** plugin-development, templating, cookies

---

## 1 answers

### Answer by username_1 on 2015-06-16. Score: 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:
Buy now!

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...)

> **Comments:**
> **username_0**: 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.  

---

