ministryofweb/analytics-campaign-urls

Adds Campaign parameters to URLs

3.0.0 2023-12-05 15:50 UTC

README

This library allows it to easily append campaign parameters (e.g. for Google Analytics, Matomo/Piwik or Plausible) to any given URL.

Requirements

At least PHP 8.1 is required to use this library in the current version.

Version 2.x of this library supports PHP versions 7.0 or higher.

Installation

composer require ministryofweb/analytics-campaign-urls

Usage

<?php

use MinistryOfWeb\AnalyticsCampaignUrls\Url;
use MinistryOfWeb\AnalyticsCampaignUrls\Parameters\GoogleAnalytics;
use MinistryOfWeb\AnalyticsCampaignUrls\Parameters\Matomo;

$params = new GoogleAnalytics(
    'summer',
    'banner',
    'website',
);

$campaignUrlGA = Url::addAnalyticsCampaignParams('https://example.com/', $params);
// → https://example.com/?utm_campaign=summer&utm_medium=banner&utm_source=website

$campaignUrlGAWithExistingParam = Url::addAnalyticsCampaignParams('https://example.com/?existingparam=1', $params);
// → https://example.com/?existingparam=1&utm_campaign=summer&utm_medium=banner&utm_source=website

$matomoParams = new Matomo(
    'summer',
    'banner',
    'website',
    'keyword',
    'content'
);

$campaignUrlMatomo = Url::addAnalyticsCampaignParams('https://example.com/', $matomoParams);
// → https://example.com/?pk_campaign=summer&pk_medium=banner&pk_source=website&pk_kwd=keyword&pk_content=content

Links