PHPackages                             bit-part/craft-code-snippets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bit-part/craft-code-snippets

ActiveCraft-plugin[Utility &amp; Helpers](/categories/utility)

bit-part/craft-code-snippets
============================

Manage and inject code snippets (GTM, Analytics, ad tags, etc.) into your Craft CMS site from the control panel.

1.0.0(1mo ago)00proprietaryPHPPHP ^8.2

Since Apr 24Pushed 1mo agoCompare

[ Source](https://github.com/bit-part/craft-code-snippets)[ Packagist](https://packagist.org/packages/bit-part/craft-code-snippets)[ RSS](/packages/bit-part-craft-code-snippets/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Code Snippets for Craft CMS 5
=============================

[](#code-snippets-for-craft-cms-5)

**[日本語](README.ja.md) | [Deutsch](README.de.md)**

Manage and inject code snippets (GTM, Analytics, ad tags, chat widgets, etc.) into your Craft CMS site directly from the control panel — no template editing required.

Requirements
------------

[](#requirements)

- Craft CMS 5.0.0 or later
- PHP 8.2 or later

Installation
------------

[](#installation)

### Via Composer (recommended)

[](#via-composer-recommended)

```
composer require bit-part/craft-code-snippets
```

Then install the plugin from the Craft control panel under **Settings &gt; Plugins**, or via the CLI:

```
php craft plugin/install code-snippets
```

### Manual Installation

[](#manual-installation)

1. Download the release from [GitHub](https://github.com/bit-part/craft-code-snippets)
2. Place the contents in a directory and add a [path repository](https://getcomposer.org/doc/05-repositories.md#path) to your project's `composer.json`
3. Run `composer require bit-part/craft-code-snippets`
4. Install via the control panel or CLI

How It Works
------------

[](#how-it-works)

Code Snippets automatically injects your managed code into the rendered HTML output. No template changes are needed by default.

1. Add a snippet in the control panel (e.g., a Google Analytics tracking script)
2. Choose an insertion position: `` start, `` end, `` start, or `` end
3. Optionally restrict by environment (dev, staging, production) or URI pattern
4. The plugin injects active snippets into every matching page

Features
--------

[](#features)

### Snippet Management

[](#snippet-management)

A dedicated **Code Snippets** section in the control panel lets you:

- Create, edit, and delete snippets
- Enable/disable snippets with bulk actions
- Duplicate existing snippets for quick setup across environments
- Set insertion position (4 positions available)
- Filter snippets by position using the sidebar
- Search, sort, and export snippets

The snippet list uses Craft's native element index, providing the same familiar interface as Entries and Assets.

### Insertion Positions

[](#insertion-positions)

PositionInserts atTypical use`` StartRight after ``GTM, high-priority scripts`` EndRight before ``Analytics, meta tags`` StartRight after ``GTM noscript fallback`` EndRight before ``Chat widgets, deferred scripts### Auto-Injection (Default)

[](#auto-injection-default)

Snippets are automatically injected into rendered HTML without any template changes. The plugin hooks into Craft's template rendering pipeline and inserts your code at the correct positions.

This is ideal for marketers and content editors who need to add tracking codes without developer assistance.

### Twig Functions (Optional)

[](#twig-functions-optional)

For developers who prefer explicit control, disable auto-injection in settings and use Twig functions in your layout template:

```
{{ craft.codeSnippets.headBegin() }}
{{ craft.codeSnippets.headEnd() }}
{{ craft.codeSnippets.bodyBegin() }}
{{ craft.codeSnippets.bodyEnd() }}
```

### Environment Filtering

[](#environment-filtering)

Restrict snippets to specific environments. For example, keep debug scripts on `dev` only, or ensure production tracking codes don't fire in development.

Select one or more environments (dev, staging, production) per snippet. Leave all unchecked to run on all environments.

### URI Pattern Matching

[](#uri-pattern-matching)

Target specific pages using URI patterns with wildcard support:

PatternMatches`checkout``/checkout` only`products/*``/products/shoes`, `/products/hats/red`, etc.`*`All pages(empty)All pagesEnter one pattern per line. Leave empty to match all pages.

### Sort Order

[](#sort-order)

Control the order of multiple snippets in the same position using the sort order field. Lower numbers appear first.

Settings
--------

[](#settings)

Navigate to **Settings &gt; Plugins &gt; Code Snippets** to configure:

SettingDefaultDescription**Auto Inject**`On`Automatically inject snippets into rendered HTML. When disabled, use the Twig functions instead.Permissions
-----------

[](#permissions)

The plugin registers two permissions under **Code Snippets**:

PermissionDescription**View snippets**Access the Code Snippets section in the control panel**Manage snippets**Create, edit, delete, and toggle snippets. **Note:** This permission allows injecting arbitrary HTML/JavaScript into the site. Grant only to trusted users.Permissions are nested: "Manage snippets" requires "View snippets" to be granted first.

Security
--------

[](#security)

This plugin intentionally outputs user-provided HTML/JavaScript code on the frontend. This is the core functionality — similar to Google Tag Manager, WordPress Header &amp; Footer Scripts, and other snippet injection tools.

**Important:** Only grant the "Manage snippets" permission to trusted administrators. Users with this permission can inject arbitrary scripts into your site.

Translations
------------

[](#translations)

The plugin includes translations for:

- English (`en`)
- Japanese (`ja`)

Support
-------

[](#support)

- [GitHub Issues](https://github.com/bit-part/craft-code-snippets/issues)
- [Documentation](https://github.com/bit-part/craft-code-snippets)

License
-------

[](#license)

This plugin is licensed under the [Craft License](LICENSE.md).

---

Built by [bit part LLC](https://bit-part.net)

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

46d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cebdb7172bc7dc536973eac841b10067e87d06196a00dff99542ec039afa6cdd?d=identicon)[bit-part](/maintainers/bit-part)

---

Top Contributors

[![tinybeans](https://avatars.githubusercontent.com/u/181721?v=4)](https://github.com/tinybeans "tinybeans (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bit-part-craft-code-snippets/health.svg)

```
[![Health](https://phpackages.com/badges/bit-part-craft-code-snippets/health.svg)](https://phpackages.com/packages/bit-part-craft-code-snippets)
```

###  Alternatives

[spicyweb/craft-neo

A Matrix-like field type with block hierarchy

393808.8k10](/packages/spicyweb-craft-neo)[craftcms/feed-me

Import content from XML, RSS, CSV or JSON feeds into entries, categories, Craft Commerce products, and more.

294943.4k27](/packages/craftcms-feed-me)[verbb/formie

The most user-friendly forms plugin for Craft.

100387.6k57](/packages/verbb-formie)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

53675.5k15](/packages/solspace-craft-freeform)[verbb/navigation

Create navigation menus for your site.

92698.4k18](/packages/verbb-navigation)[verbb/workflow

Enforce multi-step review processes for creating entries.

138123.0k1](/packages/verbb-workflow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
