How to setup AnalyticKit in Drupal?

Adding Analytickit JavaScript to a Drupal site involves a few steps. Here’s a guide to help you add the provided JavaScript code so that it appears on all pages of a Drupal website:

Step 1: Access Your Drupal Site

  1. Log in to your Drupal site: You need administrative privileges to make changes. The login URL is typically. yourwebsite.com/user.

Step 2: Decide Where to Place the Script

You have a couple of options for where to place your JavaScript code:

  • Option 1: Use a Custom Module: If you’re comfortable with Drupal development, you can create a custom module to add your JavaScript. This is a more advanced option but is best for maintainability and update compatibility.
  • Option 2: Use the Theme’s .info file: This is simpler and involves adding your script to the theme’s .info file. However, this method is theme-specific.

Option 1: Using a Custom Module

  1. Create a Custom Module: If you don’t already have one, you must create one. In your Drupal installation, go to the /modules directory and create a new folder for your module, e.g., analytickit.
  2. Create a .info File: Inside this folder, create a file named analytickit.info. Add basic details about your module:
    makefile
    name = AnalyticKit
    description = Adds AnalyticKit JavaScript to all pages.
    package = Custom
    core = 7.x
  3. Create a .module File: Create a file named analytickit.module. This is where you’ll add your script.
  4. Implement hook_page_attachments:
    function analytickit_page_attachments(array &$attachments) {
    $attachments[‘#attached’][‘html_head’][] = [
    [
    ‘#tag’ => ‘script’,
    ‘#value’ => ‘/* Your JavaScript code here */’,
    ‘#attributes’ => [‘type’ => ‘text/javascript’],
    ],
    ‘analytickit_script’,
    ];
    }
  5. Enable Your Custom Module: Go to the modules page on your Drupal site (yourwebsite.com/admin/modules) and enable your custom module.

Option 2: Using the Theme’s .info File

  1. Locate Your Theme’s Directory: Go to /themes or /sites/all/themes in your Drupal installation.
  2. Edit the .info File: Find the .info file for the theme you are using (e.g., yourtheme.info) and open it for editing.
  3. Add Your Script: Add a line to include your JavaScript file:
    scripts[] = path/to/your/script.js

    You’ll need to place your JavaScript code in a separate .js file and upload it to the specified path.

  4. Clear the Cache: After saving your changes, clear your Drupal cache to ensure the changes take effect. This can be done from the Performance page (yourwebsite.com/admin/config/development/performance).

Testing and Troubleshooting

  • After completing these steps, visit your website and check if the script is working as intended.
  • Use browser developer tools to inspect the page source and confirm that the JavaScript is loading.
  • If the script doesn’t appear, clear the cache again and check if the module is enabled (for Option 1) or if the .info file is correctly updated (for Option 2).

Note

  • The instructions above are for Drupal 7. If you’re using Drupal 8 or 9, the process will be similar, but the code syntax and file structure may differ slightly.
  • Always back up your site before making changes to the code.