Skip to main content
Contributor III
May 1, 2024
Solved

User consent through custom HTML form

  • May 1, 2024
  • 5 replies
  • 352 views

Hi, I am writing my own email form and want to have a checkbox for user consent. If the box is unchecked, the profile should be added but not subscribed.

Right now, the form works but the checkbox is ignored, so every profile gets subscribed. I have set my list to be single opt-in. What am I missing?

 


<form id="email_signup" class="klaviyo_styling klaviyo_gdpr_embed_XXXXXX email-capture__form"
action="//manage.kmail-lists.com/subscriptions/subscribe"
data-ajax-submit="//manage.kmail-lists.com/ajax/subscriptions/subscribe" method="GET" target="_blank">

<input type="hidden" name="g" value="XXXXXX">
<input type="hidden" name="$fields" value="email">

<div class="klaviyo_field_group klaviyo_form_actions">
<label for="k_id_email" class="email-capture__label">Enter your email to download a free trial.</label>
<input class="email-capture__input" type="email" value="" name="email" id="k_id_email" placeholder="email"/>
</div>

<div class="klaviyo_messages">
<div class="success_message" style="display:none;">
<div>
<h3>Download</h3>
<div class="btn-flex">
<a href="#" class="download-btn">1</a>
<a href="#" class="download-btn">2</a>
</div>
</div>
</div>
<div class="error_message" style="display:none;"></div>
</div>
<div class="klaviyo_form_actions">
<button type="submit" class="klaviyo_submit_button">SUBMIT</button>
<div class="checkbox-flex">
<input type="checkbox" name="$consent" id="consent-email" value="email" checked>
<label for="consent-email">Email me exclusive offers and free plugin updates</label>
</div>
</div>
</form>


<script type="text/javascript" src="//www.klaviyo.com/media/js/public/klaviyo_subscribe.js"></script>

<script type="text/javascript">
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}

var form = document.getElementById('email_signup');
var emailInput = form.querySelector('input[type="email"]');
var submitButton = form.querySelector('button[type="submit"]');

form.addEventListener('submit', function(e) {
if (!validateEmail(emailInput.value)) {
e.preventDefault();
form.querySelector('.error_message').innerHTML = 'Please enter a valid email address';
form.querySelector('.error_message').style.display = 'block';
}
});

KlaviyoSubscribe.attachToForms('#email_signup', {
hide_form_on_success: true,
custom_success_message: true,
extra_properties: {
$source: 'Free Trial Form'
}
});
</script>



 

    This topic has been closed for replies.
    Best answer by chrisB

    Not sure, its probably a reload issue. 

     

    There are a couple of ways to cut this, but fetch() is ok if the JS supports it (or if you want to write something else).  Im not sure if the code works with the JS file in the first instance. 

     

    If it was me, I would probably send an event with the info in the datalayer, then post this to klaviyo via GTM as I could use the API to create profiles and subscribe them to different lists etc 

    5 replies

    Problem Solver III
    May 1, 2024

    I don’t know about the form, but for the API it looks the values need to be as followed:

    $consent = SUBSCRIBED

     

    whereas you $consent is = email

     

    https://developers.klaviyo.com/en/reference/subscribe_profiles

    I sometimes know things...
    JaSnAuthor
    Contributor III
    May 2, 2024

    Thanks. The emails are still added as “subscribed”, though. What value would I need for them to say “unsubscribed” or “suppressed”?

    Problem Solver III
    May 3, 2024

    Thanks. The emails are still added as “subscribed”, though. What value would I need for them to say “unsubscribed” or “suppressed”?

     

    I think there are seperate endpoints to suppress and unsubscribe. 

     

    Not sure about the website, but in API side you would:
     

    POST / Create Profile Subscribed

    POST / Unsubscribe

    POST / Suppress 

     

    Whats the point of creating a profile that is unsubscribed later on?

     

    On a side note, the script before isusing the klaviyo_subscribe.js which (i would assume) is a mirror of my previous link. if you want to create a profile without subscribing it, I would assume you would need to make something like this endpoint

    https://developers.klaviyo.com/en/reference/create_profile

    I sometimes know things...
    JaSnAuthor
    Contributor III
    May 3, 2024

    Thanks. I ended up doing a POST call with “fetch('https://a.klaviyo.com/client/subscriptions/”. And if the checkbox is unchecked, it does not make that call. 

    It’s interesting that Firefox private tab, and Duck Duck Go browser would suppress that API call, though. Is there anything that can be done about it?

    chrisBAnswer
    Problem Solver III
    May 3, 2024

    Not sure, its probably a reload issue. 

     

    There are a couple of ways to cut this, but fetch() is ok if the JS supports it (or if you want to write something else).  Im not sure if the code works with the JS file in the first instance. 

     

    If it was me, I would probably send an event with the info in the datalayer, then post this to klaviyo via GTM as I could use the API to create profiles and subscribe them to different lists etc 

    I sometimes know things...