pixelistik / url-campaignify
一个PHP类,帮助向URL添加网站分析活动。
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-28 14:13:57 UTC
README
URL-Campaignify
背景
开源网站分析工具Piwik可以使用活动和关键词对传入链接进行分类。它们通过向HTTP URL附加额外的GET参数来实现
http://my-site.tld/?pk_campaign=newsletter-5&pk_kwd=header-link
Google analytics以及可能的大多数其他分析工具都基本上做同样的事情。
内容
此类旨在使动态向URL添加此类参数更加容易。
单个URL
无需担心?
和&
,只需这样做
$uc = new UrlCampaignify();
$url = "http://some-blog.tld/cms.php?post=123&layout=default";
$newUrl = $uc->campaignify($url, "newsletter-5", "header-link");
结果已正确添加参数
http://some-blog.tld/cms.php?post=123&layout=default&pk_campaign=newsletter-5&pk_kwd=header-link
文本块
您也可以将整个文本块传递给函数。它将找到并活动化其中的所有HTTP URL。
$uc = new UrlCampaignify();
$text = "Look at http://my-site.tld especially".
"here: http://my-site.tld/news.htm";
$newUrl = $uc->campaignify($text, "newsletter-5", "header-link");
如果您期望HTML输入,则只更改href
属性中的URL是有意义的。使用campaignifyHref()
进行此操作。它将
See <a href="http://site.tld">http://site.tld</a> for more information.
变为
See <a href="http://site.tld?pk_campaign=foo">http://site.tld</a> for more information.
查看测试用例以查看哪些情况和边缘情况已被覆盖或未被覆盖。
在文本块中自动编号URL
文本块中的所有活动化URL都将计数(从1开始)。您可以在sprintf()
样式的关键字中使用URL的当前编号。如果您想在文本中区分几个相同的URL,这很有用。
$uc = new UrlCampaignify();
$text = "Here comes the header link: http://my-site.tld".
"here is a long and verbose text".
"and another link at the end: http://my-site.tld";
$newUrl = $uc->campaignify($text, "news", "link-%d");
将给出
Here comes the header link: http://my-site.tld?pk_campaign=news&pk_kwd=link-1
here is a long and verbose text and another link at the end:
http://my-site.tld?pk_campaign=news&pk_kwd=link-2";
域名
如果您实际上分析它们,则添加活动才有意义。这意味着您控制网站及其分析工具。您可以将UrlCampaignify限制为仅处理给定域的URL。只需将其传递给构造函数即可
$uc = new UrlCampaignify('my-site.tld')
注意,子域名不会自动包含在内,因此上面的实例将不会触及www.my-site.tld
上的URL。尽管如此,您可以将多个域名指定为数组
$uc = new UrlCampaignify(array('my-site.tld', 'www.my-site.tld', 'my-other-site.tld'))
安装
使用composer
URL-Campaignify符合PSR-0文件布局,并在Packagist上。您应该能够简单地键入
composer require pixelistik/url-campaignify:dev-master
composer install
以将master分支的最新代码包含到您的项目中。
仅获取文件
您还可以直接下载提供类的单个文件:https://github.com/pixelistik/url-campaignify/raw/master/src/Pixelistik/UrlCampaignify.php