xsuchy09/utm-cookie

Utm-Cookie可以将URL中的utm参数保存到具有定义寿命的cookie中(默认7天)。之后,无需解析Google或其他任何cookie,即可使用cookie(utm)。

2.0.4 2024-08-16 17:21 UTC

This package is auto-updated.

Last update: 2024-09-16 17:27:31 UTC


README

PHP库,用于将URL中的utm参数保存到cookie中以便以后使用。版本2.0.0+需要PHP 7.1。如需PHP 5.4+兼容性,请使用1.0.6版本。

作者

概述

UtmCookie可以将URL中的utm参数保存到具有定义寿命的cookie中(默认7天)。之后,无需解析Google或其他任何cookie,即可使用cookie(utm)。

它处理以下utm参数:

  • utm_campaign
  • utm_medium
  • utm_source
  • utm_term
  • utm_content

您可以使用原始名称或去掉"utm_"(例如,对于"utm_source",只需"source" - 您可以使用两者) - 请参见示例。

从版本2.0.2开始,您可以通过调用UtmCookie::save($array)来重写这些cookie,其中$array应包含由UtmCookie::$allowedUtmCookieKeys允许的键(默认是允许的utm参数名称)。

安装(通过composer)

获取composer并在composer.json的require部分添加以下内容

{
    "require": {
        "xsuchy09/utm-cookie": "*"
    }
}

然后

composer install

使用方法

基本示例

UtmCookie::init(); // just init - read utm params and cookie and save new values (is auto called by first call of UtmCookie::get method)
UtmCookie::get(); // get all utm cookies as array
UtmCookie::getObject(); // get all utm cookies as object (stdClass)
UtmCookie::get('utm_source'); // get utm_source
UtmCookie::get('source'); // get utm_source

设置utm cookie的寿命

$dateInterval = DateInterval::createFromDateString('7 days');
UtmCookie::setLifetime($dateInterval);

设置utm cookie的名称

UtmCookie::setName('utm');

设置是否覆盖所有utm值,即使只检测到一个。

默认为TRUE。如果设置为false,则只有在设置的情况下才覆盖utm值(其他值将保持不变)。

UtmCookie::setOverwrite(false);

更多示例可以在examples/目录中找到。