Link Search Menu Expand Document

Trigger behavior during a customer’s birthday month

Estimated time to implement: 45 minutes, difficulty: 5/10

When I give a company my birthday, I would expect them to somehow recognize it. This example will show you how to do just that!

Step one: render the snippet

Navigate to layout/theme.liquid in your theme editor, then add the following code inside the <head> tag:

{% render 'customer-fields', customer_api: true, version: '<Version number>' %}

Step two: create a snippet to be used globally

We want to perform this logic wherever a customer can land on the storefront. The best way to do this is by injecting code in the <head> in layout/theme.liquid, just as we do when rendering the customer-fields snippet.

Create a snippet called handle-customer-birthday, then render it right below the customer-fields snippet:

{% render 'customer-fields', customer_api: true, version: '<Version number>' %}

{% render 'handle-customer-birthday' %}

Step three: Prepare JavaScript

In your handle-customer-birthday snippet, add the following code:

<script>
  CF.customerReady(function() {
    // Ready to handle customer data!
  });
</script>

The CF.customerReady function provides you with an easy way to ensure your code will only run when our code is ready.

Step four: Determine if it’s the customer’s birthday month

For the sake of this example, we’ll assume you have a data column with a key of birthday, and a data type of Date.

Use the CF.customer.get function to obtain the customer’s birthday:

<script>
  CF.customerReady(function() {
    // Ready to handle customer data!
    var birthday = CF.customer.get('birthday');

    console.log(birthday); // -> 1998-06-08
  });
</script>

Check to see if the data actually exists, since some customers may not have the birthday in their data:

<script>
  CF.customerReady(function() {
    // Ready to handle customer data!
    var birthday = CF.customer.get('birthday');
    if (!birthday) return;

    console.log(birthday); // -> 1998-06-08
  });
</script>

You’ll see that the data you get is a string, in the format of YYYY-MM-DD. Don’t create a Date with this data, or it will be a day behind.

CF.customer contains a method you can use to parse the value as a dayjs instance. This function needs to be called before you get any customer data.

<script>
  CF.customerReady(function() {
    // Convert birthday from a string to a dayjs instance
    CF.customer.parseDates('birthday');
    var birthday = CF.customer.get('birthday');
    if (!birthday) return;

    console.log(birthday); // -> c {$L: "en", $d: Mon Jun 08 1998 00:00:00 GMT-0700 (Pacific Daylight Time), $y: 1998, $M: 5, $D: 8, …}
  });
</script>

Now birthday is not a string. It’s a dayjs instance which contains many helpful tools we can use to analyze its value. Learn more about dayjs instances and what you can do with them.

Step five: check to see if the current month is the customer’s birthday month

Extract the month from birthday

Luckily for us, dayjs provides a very simple API we can use to get the month:

<script>
  CF.customerReady(function() {
    // Convert birthday from a string to a dayjs instance
    CF.customer.parseDates('birthday');
    var birthday = CF.customer.get('birthday');
    if (!birthday) return;

    var month = birthday.month(); // 0 = January, 1 = Feburary, ... 11 = December
  });
</script>

Get the current month

We expose dayjs in the CF namespace so you can use it. To initialize an instance, simply call it as a function:

var today = CF.dayjs();

You can get today’s month exactly how you did for birthday:

var today = CF.dayjs();
var thisMonth = today.month();

But we don’t really need both variables, so let’s condense it to a single statement:

var thisMonth = CF.dayjs().month();

Now add it to your script:

<script>
  CF.customerReady(function() {
    // Convert birthday from a string to a dayjs instance
    CF.customer.parseDates('birthday');
    var birthday = CF.customer.get('birthday');
    if (!birthday) return;

    var month = birthday.month(); // 0 = January, 1 = Feburary, ... 11 = December
    var thisMonth = CF.dayjs().month();
  });
</script>

Check for equality

The variables month and thisMonth are simple numbers. You can check for equality simply by using ==:

<script>
  CF.customerReady(function() {
    // Convert birthday from a string to a dayjs instance
    CF.customer.parseDates('birthday');
    var birthday = CF.customer.get('birthday');
    if (!birthday) return;

    var month = birthday.month(); // 0 = January, 1 = Feburary, ... 11 = December
    var thisMonth = CF.dayjs().month();

    if (month == thisMonth) {
      // It's the customer's birthday month!
    } else {
      // It's not :(
    }
  });
</script>

Next steps

How you want to handle the birthday is up to you. Inside the month == thisMonth condition, you may want to:

  • Redirect to a page offering a discount
  • Display a banner at the top of the page
  • Add a free gift to the cart using Cart.js

Remember, this will happen on every page loaded within the customer’s birthday month. If you only wish to invoke certain functionality once, make sure to set some sort of flag on the customer so you can determine whether or not any functionality should happen.


Have any questions or comments about this post? Let us know! Your feedback is greatly appreciated.

Customer Fields is a Shopify app made by Helium.

Copyright © 2020 Helium Development, LLC