Using Feature Flags with Sculpin

Background

I was asked last week to add a new feature, a Facebook pixel for measuring and building advertising campaigns, to a client's website which I built using the Sculpin static site generator.

The site uses settings within the app/config/sculpin_site.yml file for storing site IDs and usernames. For this, I would add something like:

facebook:
    pixel:
        id: "abc123"

It can then be retrieved with .

If I then needed to disable the pixel, then I'd typically remove the pixel ID:

facebook:
    pixel:
        id: ~

Introducing feature flags

A technique that I like to use on other projects is using feature flags (aka feature toggles).

Whilst, in this instance, a feature flag wouldn't separate deploying code from toggling a feature - a static site will need to be re-generated and deployed - I thought that there was value in being able to easily toggle a feature without changing its configuration or removing code within the site's templates.

Feature flags in Sculpin

My Sculpin feature flag implementation was to add a feature_flags key within sculpin_site.yml, with each feature's name as the key and a boolean value to set whether it's enabled - similar to how the Drupal 7 version of the Feature Toggle module works.

This is how I added the Facebook pixel feature flag:

feature_flags:
    add_facebook_pixel: true

Using the Facebook pixel feature flag

The Facebook pixel code is stored within it's own partial that I can include from my source/_layouts/app.html.twig layout, including the pixel ID and whether or not the feature flag is enabled.


{% include "facebook-pixel" with {


    is_enabled: site.feature_flags.add_facebook_pixel,
    pixel_id: site.facebook.pixel.id,
} only %}

Within the partial, I can check that both the feature flag is enabled and that there is a Facebook pixel ID, and only add the pixel code if both conditions return a truthy value.

Now the pixel can be removed just by setting add_facebook_pixel: false in sculpin_site.yml, and without changing any other configuration or templates.

Was this useful?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.