Setup Snappy in Drupal 9
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
- Access Drupal administration
- Method 1: Using the Asset Injector module
- Method 2: Adding script to theme
- Verifying installation
- Troubleshooting
Access Drupal Administration
To get started with Snappy on your Drupal 9 site:
- Log in to your Drupal administration panel
- 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:
-
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
-
Navigate to Configuration → Development → Asset Injector in your Drupal admin
-
Click on + Add JavaScript Injector
-
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)
-
Make sure to replace
YOUR_DOMAIN
with the domain the script should be installed on -
Click Save to apply changes
Method 2: Adding Script to Theme
If you prefer modifying your theme directly:
-
Navigate to your active theme's directory in your Drupal installation
/themes/custom/YOUR_THEME_NAME/
-
Open the theme's
.info.yml
file and ensure libraries are defined:libraries: - YOUR_THEME_NAME/global-styling - YOUR_THEME_NAME/snappy-integration
-
Create or edit the
YOUR_THEME_NAME.libraries.yml
file to add:snappy-integration: js: js/snappy.js: { attributes: { async: true } }
-
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); })();
-
Make sure to replace
YOUR_DOMAIN
with the domain the script should be installed on -
Clear Drupal's cache:
drush cache:rebuild
Or via the admin UI: Configuration → Development → Performance → Clear all caches
Verifying Installation
To confirm Snappy is working correctly:
- Visit your Drupal 9 site in a web browser
- Right-click and select View Page Source
- Search (Ctrl+F or Cmd+F) for "letsbesnappy.com" - you should see your script in the head section
- 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:
- Check for JavaScript aggregation in Configuration → Development → Performance
- Temporarily disable JavaScript optimization features to test
- 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:
- Consider creating a sub-theme to avoid losing changes during updates
- Alternatively, use the Asset Injector method which doesn't modify theme files directly
- 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!