antimattr/google-bundle

此包已被弃用且不再维护。未建议替代包。

Symfony2 的 Google Bundle

安装次数: 168,263

依赖者: 3

建议者: 0

安全: 0

星标: 239

关注者: 12

分支: 101

开放问题: 37

类型:symfony-bundle

v2.0.4 2015-04-01 17:57 UTC

README

GoogleBundle 允许您将各种 Google 服务添加到您的应用程序中。这包括 Google 分析、Adwords 和静态地图。

安装

将以下内容添加到您的 composer.json 文件中

{
    "require": {
        "antimattr/google-bundle": "~2.0@stable"
    }
}

通过运行以下命令安装库

composer install

如果一切顺利,Google Bundle 现在可以在 vendor/antimattr/google-bundle 中找到。

最后,确保在 AppKernel.php 中启用该包,如下所示

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        //...
        new AntiMattr\GoogleBundle\GoogleBundle(),
    );
}

配置

Google 分析

google:
    analytics:
        enhanced_ecommerce: false
        session_auto_started: true
        trackers:
            default:
                name: MyJavaScriptCompatibleVariableNameWithNoSpaces
                accountId: UA-xxxxxx-xx
                domain: .mdomain.com
                setSiteSpeedSampleRate: 5
                allowAnchor: true
                allowHash: true
                includeNamePrefix: false
                plugins:
                    - 'linkid'
        whitelist: [ 'q', 'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign' ]

视图

head 标签或布局的 </body> 之前包含 Google 分析异步模板(模板将按需加载 _gaq)。

使用 twig

{% include "GoogleBundle:Analytics:async_universal.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);

增强电子商务跟踪

衡量交易

https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transactions

$transaction = new \AntiMattr\GoogleBundle\Analytics\Transaction();
$transaction->setOrderNumber('xxxx');
$transaction->setAffiliation('Store 777');
$transaction->setRevenue(100.00); // <== NEW    
$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);

$product = new \AntiMattr\GoogleBundle\Analytics\Item();
$product->setSku('zzzz');
$product->setTitle('Product X');
$product->setAction('purchase');    
$product->setBrand('Brand AA');
$product->setCategory('Category A');
$product->setPrice(50.00);
$product->setQuantity(1);
$product->setVariant('Black');
$product->setCoupon('COUPON AAA');
$product->setPosition(1);
$this->get('google.analytics')->addItem($product);

$product = new \AntiMattr\GoogleBundle\Analytics\Item();
$product->setOrderNumber('bbbb');
$product->setSku('jjjj');
$product->setTitle('Product Y');
$product->setAction('purchase');    
$product->setBrand('Brand BB');    
$product->setCategory('Category B');
$product->setPrice(25.00);
$product->setQuantity(2);
$product->setVariant('Yellow');
$product->setCoupon('COUPON BBB');
$product->setPosition(2);    
$this->get('google.analytics')->addItem($product);

衡量展示

https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-impressions

$impression = new \AntiMattr\GoogleBundle\Analytics\Impression();
$impression->setSku('zzzz');
$impression->setTitle('Product X');
$impression->setAction('detail');    
$impression->setBrand('Brand AA');
$impression->setCategory('Category A');
$impression->setPrice(50.00);
$impression->setVariant('Black');
$impression->setList('Search Results Page 1');
$impression->setPosition(1);
$this->get('google.analytics')->addImpression($impression);

$impression = new \AntiMattr\GoogleBundle\Analytics\Impression();
$impression->setOrderNumber('bbbb');
$impression->setSku('jjjj');
$impression->setTitle('Product Y');
$impression->setAction('detail');    
$impression->setBrand('Brand BB');    
$impression->setCategory('Category B');
$impression->setPrice(25.00);
$impression->setVariant('Yellow');
$impression->setList('Search Results Page 2');
$impression->setPosition(2);    
$this->get('google.analytics')->addImpression($impression);

Google Adwords

应用配置 config.yml

通过将以下内容添加到应用的 config.yml 文件中,启用加载 Google Adwords 服务

google:
    adwords:
        conversions:
            account_create:
                id:    111111
                label: accountCreateLabel
                value: 0
            checkout_thanks:
                id:    222222
                label: checkoutThanksLabel
                value: 0

控制器

$this->get('google.adwords')->activateConversionByKey('account_create');

视图

像这样包含 Google Adwords 跟踪模板

{% include "GoogleBundle:Adwords:track.html.twig" %}

Google 地图 - 静态地图

应用配置 config.yml

通过将以下内容添加到应用的 config.yml 文件中,启用加载 Google Maps 静态服务(静态服务不需要 API 密钥)

google:
    maps: ~

控制器

use AntiMattr\GoogleBundle\Maps\StaticMap;
use AntiMattr\GoogleBundle\Maps\Marker;

...

$map = new StaticMap();
$map->setId("Paul");
$map->setSize("512x512");
$marker = new Marker();
$marker->setLatitude(40.596631);
$marker->setLongitude(-73.972359);
$map->addMarker($marker);
$this->container->get('google.maps')->addMap($map);

视图

像这样在模板中包含 Google 地图

{% if google_maps.hasMaps() %}
	{% for map in google_maps.getMaps() %}
		{% autoescape false %}
			{{ map.render }}
		{% endautoescape %}
	{% endfor %}
{% endif %}