Setup Snappy in Drupal 9

Published on

Install Snappy in Drupal 9

Adding Snappy to your Drupal 9 site gives your marketing team the freedom to optimize and test without waiting for development resources. The installation is straightforward and requires minimal technical effort.

Overview

  1. Access Drupal administration
  2. Method 1: Using the Asset Injector module
  3. Method 2: Adding script to theme
  4. Verifying installation
  5. Troubleshooting

Access Drupal Administration

To get started with Snappy on your Drupal 9 site:

  1. Log in to your Drupal administration panel
  2. Ensure you have administrative permissions to modify code or install modules

Method 1: Using the Asset Injector Module

The easiest way to install Snappy on Drupal 9 is using the Asset Injector module:

  1. Install the Asset Injector module if not already installed

    composer require drupal/asset_injector:^2.7
    drush en asset_injector
    

    Note: Version 2.7+ is recommended for Drupal 9 compatibility

  2. Navigate to ConfigurationDevelopmentAsset Injector in your Drupal admin

  3. Click on + Add JavaScript Injector

  4. Fill in the following details:

    • Label: Snappy Integration
    • Code: Paste your Snappy script:
    <script src="https://scripts.letsbesnappy.com/YOUR_DOMAIN/admin.js"></script>
    
    • Placement: Select "Inside HEAD tag"
    • Pages: Leave blank to add to all pages (or specify URLs to restrict to certain pages)
  5. Make sure to replace YOUR_DOMAIN with the domain the script should be installed on

  6. Click Save to apply changes

Method 2: Adding Script to Theme

If you prefer modifying your theme directly:

  1. Navigate to your active theme's directory in your Drupal installation

    /themes/custom/YOUR_THEME_NAME/
    
  2. Open the theme's .info.yml file and ensure libraries are defined:

    libraries:
      - YOUR_THEME_NAME/global-styling
      - YOUR_THEME_NAME/snappy-integration
    
  3. Create or edit the YOUR_THEME_NAME.libraries.yml file to add:

    snappy-integration:
      js:
        js/snappy.js: { attributes: { async: true } }
    
  4. Create file js/snappy.js with:

    (function() {
      var script = document.createElement('script');
      script.src = "https://scripts.letsbesnappy.com/YOUR_DOMAIN/admin.js";
      document.head.appendChild(script);
    })();
    
  5. Make sure to replace YOUR_DOMAIN with the domain the script should be installed on

  6. Clear Drupal's cache:

    drush cache:rebuild
    

    Or via the admin UI: ConfigurationDevelopmentPerformanceClear all caches

Verifying Installation

To confirm Snappy is working correctly:

  1. Visit your Drupal 9 site in a web browser
  2. Right-click and select View Page Source
  3. Search (Ctrl+F or Cmd+F) for "letsbesnappy.com" - you should see your script in the head section
  4. Log in to your Snappy dashboard - your Drupal site should appear as a connected site

Troubleshooting

If you encounter issues with your Snappy installation:

Script not loading on all pages

Drupal uses different templates and caching mechanisms that may affect script loading:

  • Ensure your Asset Injector settings don't exclude certain pages
  • Check if you have page caching enabled and clear all caches
  • If using a theme-based approach, verify your libraries are correctly registered

Conflicts with other modules

Some Drupal modules that handle JavaScript might interfere with custom scripts:

  1. Check for JavaScript aggregation in ConfigurationDevelopmentPerformance
  2. Temporarily disable JavaScript optimization features to test
  3. Look for JavaScript errors in your browser's console (F12 or Cmd+Option+I)

Bartik or other core themes

If you're using Drupal 9's default Bartik theme or other core themes:

  1. Consider creating a sub-theme to avoid losing changes during updates
  2. Alternatively, use the Asset Injector method which doesn't modify theme files directly
  3. If you must modify core themes, document your changes for future reference

With Snappy successfully installed on your Drupal 9 site, you're ready to start creating widgets, conducting A/B tests, and optimizing your site without waiting for development cycles to complete.

Ready to transform your Drupal site? Login to your Snappy dashboard and begin creating your first optimization today!