Solved

How to track across subdomains (checkout on a different page)?

  • 22 April 2021
  • 7 replies
  • 1277 views

  • Anonymous
  • 0 replies

Hello,
I have my website tracking set up on both example.com and checkout.example.com

However; once the user move from add to cart to initiate checkout; it is not identified anymore (I assume the cookies are not shared across the subdomains).
How can I solve this and keep identifying the user on the checkout?

icon

Best answer by Dov 9 September 2021, 18:06

View original

7 replies

Thank you for your answer. I am indeed using a custom solution.
This is mostly for the event “initiate checkout”. We only have one link from the main website to checkout (in the cart page) so I’ll use a bit of the code to change that link with the email parameter if the user is identified (inspired from your solution)

Badge +1

Hi Dov

Already seen that. The problem is that this line does not seem to work anymore:

 _learnq.identify().$email

It simply returns undefined 

Userlevel 7
Badge +61

Hi @Dasost,

Thanks for the follow-up.

This is due to some security changes we recently put into place. Previously, our email-to-website tracking included a base64 encoded parameter with email and public api key included in it. This was called the _ke parameter. We have since switched to using an exchange id, or _kx  parameter on these urls, which is a hashed user id that only Klaviyo can decode which is far more secure than the previous method. What this means in practice is that _learnq.identify().$email will no longer work since we do not expose a raw email address to the cookie. Instead, however, we could do something similar to the code given above, but append the exchange id value to each link on one subdomain that then directs to the other.

To retrieve the exchange_id you can either decode the klaviyo cookie and retrieve it, or you can use a method we expose in our frontend library called _learnq.push(["_getIdentifiers"]) . This should return (possibly amongst some other things), a key value pair in an object with $exchange_id so long as the user is cookied. From the above script provided, we'd just want to edit it to include using the _kx parameter, rather than the _ke parameter, and use _learnq.push(["_getIdentifiers"]).$exchange_id  instead of _learnq.identify().$email  while also removing the encoding. I have yet to test this out myself, but I believe the following should take care of the issues you're seeing:

<script>
setTimeout(function(){
if (typeof _learnq !== 'undefined'){
var kl_account = _learnq.account();
var exchange_id = _learnq.push(["_getIdentifiers"]).$exchange_id;
if (exchange_id){
$(document).find('a').each(function(){
if( typeof $(this) !== 'undefined' && typeof $(this).attr('href') !== 'undefined' &&
$(this).attr('href').indexOf('store.') != -1){
var _href = $(this).attr('href');
var param_connector = _href.indexOf('?') == -1 ? '?' : '&'
$(this).attr('href', _href + param_connector + '_kx=' + exchange_id)
}
});
}
}
}, 1200);
</script>

Thanks for being a member of our community.

To add to that: as I see on my website the cookie is set for the domain www.example.com; is it possible to set it up for “.example.com” instead? Or would that mess it up for those who have different subdomains for different Klaviyo account?

Userlevel 7
Badge +61

**UPDATE: this script no longer works. Please see the best reply in this thread for an updated version of the code. That post is also highlighted at the top of the thread. Thank you.

-The Klaviyo Community Team

Hi @Hugo,

Thank you for sharing your question with the Klaviyo Community.

Cookies shouldn't be an issue for subdomains unless they are hosted in different places, for example if your root domain is hosted on Shopify and your subdomain is hosted on Wix.

If these two domains are hosted separately, there is a workaround. You’d add the following snippet to the footer of site A site to attach the _ke parameter to all links with the word "store." in the url:

<script>

  setTimeout(function(){

    if (typeof _learnq !== 'undefined'){

      var kl_account = _learnq.account();

      var kl_email = _learnq.identify().$email;

      if (kl_email){

        var info = encodeURIComponent(btoa(JSON.stringify({ "kl_email": kl_email, "kl_company_id":kl_account})));

        $(document).find('a').each(function(){

          if( typeof $(this) !== 'undefined' &&

              typeof $(this).attr('href') !== 'undefined' &&

              $(this).attr('href').indexOf('store.') != -1){

            var _href = $(this).attr('href');

            var param_connector = _href.indexOf('?') == -1 ? '?' : '&'

            $(this).attr('href', _href + param_connector + '_ke=' + info)

          }

        });

      }

    }

  }, 1200);

</script>

 

You would then add the following snippet to the footer of site B to attach the _ke parameter to all links with the word "blog." in the url:

<script>

  setTimeout(function(){

    if (typeof _learnq !== 'undefined'){

      var kl_account = _learnq.account();

      var kl_email = _learnq.identify().$email;

      if (kl_email){

        var info = encodeURIComponent(btoa(JSON.stringify({ "kl_email": kl_email, "kl_company_id":kl_account})));

        $(document).find('a').each(function(){

          if( typeof $(this) !== 'undefined' &&

              typeof $(this).attr('href') !== 'undefined' &&

              $(this).attr('href').indexOf('blog.') != -1){

            var _href = $(this).attr('href');

            var param_connector = _href.indexOf('?') == -1 ? '?' : '&'

            $(this).attr('href', _href + param_connector + '_ke=' + info)

          }

        });

      }

    }

  }, 1200);

</script>

 

With that said, the actual checkout portion does not rely on cookies like an Added to Cart event does. Each ecommerce integration has its own “threshold” for checkout tracking (logging a Started Checkout event). You can find them below for the our standard ecommerce integrations:

  1. Shopify
  2. Bigcommerce,
  3. WooCommerce 
  4. Magento 1 
  5. Magento 2

If you have a custom set-up, we also have a document for tracking checkouts.

I also do not believe it is possible to set-up cookie tracking for a partial domain, like .example.com. If the end-goal for this question is the same (trying to track both domain and sub-domain) then the above solution should work.

Thanks and have a great weekend.

Badge +1

Hi Klaviyo

 

I can not make this code work anymore. This line just gives me “undefined” right now. Has anything changed?

var kl_email = _learnq.identify().$email

 

Best regards 
Thomas

Userlevel 7
Badge +61

Hi @Dasost,

Thank you for your reply.

There was a previous community thread speaking to issues with that code. Can you try the suggestions outlined here?

 

Reply