meinfernbus / google-bundle
AntiMattr GoogleBundle 的分支。具有缓存的静态谷歌地图、 remarketing 广告以及许多其他修复。
v0.2.2
2020-12-24 11:12 UTC
Requires
- php: ^7.2
- symfony/config: ^3.4 || ^4.4 || ^5.0
- symfony/dependency-injection: ^3.4 || ^4.4 || ^5.0
- symfony/http-kernel: ^3.4 || ^4.4 || ^5.0
Requires (Dev)
- irstea/php-cs-fixer-shim: ^2.16
- phpunit/phpunit: ^8.5 || ^9.4
- symfony/browser-kit: ^3.4 || ^4.4 || ^5.0
- symfony/framework-bundle: ^3.4 || ^4.4 || ^5.0
- symfony/twig-bundle: ^3.4 || ^4.4 || ^5.0
- symfony/yaml: ^3.4 || ^4.4 || ^5.0
README
GoogleBundle
GoogleBundle 允许将各种与谷歌相关的服务添加到您的应用程序中。这些包括谷歌分析、Adwords 和静态地图。这个模块很久以前从 https://github.com/antimattr/GoogleBundle 分支出来,但现在作为独立项目维护。
安装
Composer
composer require meinfernbus/google-bundle
应用程序内核
将 GoogleBundle 添加到您的应用程序内核的 registerBundles()
方法中
public function registerBundles() { return array( new AntiMattr\GoogleBundle\GoogleBundle(), ); }
配置
谷歌分析
应用程序配置文件
通过添加以下内容到应用程序的 config.yml
文件中启用加载谷歌分析服务:
google: analytics: trackers: default: name: MyJavaScriptCompatibleVariableNameWithNoSpaces accountId: UA-xxxx-x domain: .mydomain.com trackPageLoadTime: true anonymizeIp: false
视图
在 head
标签中包含谷歌分析异步模板,或者在其布局的 </body>
标签之前(模板将懒加载 _gaq)。
使用 twig
{% include "GoogleBundle:Analytics:async.html.twig" %}
功能
记录默认页面视图
Requires no additional code
发送自定义页面视图
$this->container()->get('google.analytics')->setCustomPageView('/profile/'.$username);
添加到页面视图队列
注意:页面视图队列总是在自定义页面视图之前执行
$this->container()->get('google.analytics')->enqueuePageView('/my-first-page-view-in-queue'); $this->container()->get('google.analytics')->enqueuePageView('/my-second-page-view-in-queue');
电子商务跟踪
$transaction = new \AntiMattr\GoogleBundle\Analytics\Transaction(); $transaction->setOrderNumber('xxxx'); $transaction->setAffiliation('Store 777'); $transaction->setTotal(100.00); $transaction->setTax(10.00); $transaction->setShipping(5.00); $transaction->setCity("NYC"); $transaction->setState("NY"); $transaction->setCountry("USA"); $this->get('google.analytics')->setTransaction($transaction); $item = new \AntiMattr\GoogleBundle\Analytics\Item(); $item->setOrderNumber('xxxx'); $item->setSku('zzzz'); $item->setName('Product X'); $item->setCategory('Category A'); $item->setPrice(50.00); $item->setQuantity(1); $this->get('google.analytics')->addItem($item); $item = new \AntiMattr\GoogleBundle\Analytics\Item(); $item->setOrderNumber('bbbb'); $item->setSku('jjjj'); $item->setName('Product Y'); $item->setCategory('Category B'); $item->setPrice(25.00); $item->setQuantity(2); $this->get('google.analytics')->addItem($item);
谷歌 Adwords
应用程序配置文件
通过在应用程序的 config.yml
文件中添加以下内容来启用加载谷歌 Adwords 服务:
google: adwords: conversions: account_create: id: 111111 label: accountCreateLabel value: 0 remarketing: false checkout_thanks: id: 222222 label: checkoutThanksLabel value: 0 remarketing: false remarketing: id: 333333 label: "google-assigned-remarketing-label" value: 0 remarketing: true
控制器
$this->get('google.adwords')->activateConversionByKey('account_create');
视图
如下所示包含谷歌 Adwords 跟踪模板
{% include "GoogleBundle:Adwords:track.html.twig" %}
谷歌地图 - 静态地图
应用程序配置文件
通过在应用程序的 config.yml
文件中添加以下内容来启用加载谷歌地图静态服务:
google: maps: config: key: YOUR-API-KEY-FROM-GOOGLE host: YOUR-HOST // Host where the gmap image is served. (Optional, only needed if script is running from CLI. If not set $_SERVER is used) uploadDir: '%kernel.project_dir%/web/maps' // absolute path to directory where map files publicDir: '/maps' //http path where map files supposed to be publicly available
在 https://code.google.com/apis/console/ 获取您的密钥
控制器
use AntiMattr\GoogleBundle\Maps\StaticMap; use AntiMattr\GoogleBundle\Maps\Marker; ... /** @var \AntiMattr\GoogleBundle\MapsManager $googleContainer */ $googleContainer = $this->container->get('google.maps'); $map = $googleContainer->createStaticMap(); $map->setId("Paul"); $map->setSize("512x512"); $marker = new Marker(); $marker->setLatitude(40.596631); $marker->setLongitude(-73.972359); $map->addMarker($marker); $googleContainer->addMap($map);
视图
如下所示在您的模板中包含谷歌地图
{% if google_maps.hasMaps() %} {% for map in google_maps.getMaps() %} {% autoescape false %} {{ map.render }} {% endautoescape %} {% endfor %} {% endif %}