xsolve-pl / google-bundle
antimattr/google-bundle和drymek/google-bundle的分支,增加了JavaScript地图功能。
dev-fix/analytics-allow-optional-event-args
2012-07-17 15:10 UTC
Requires
- php: >=5.3.0
- symfony/symfony: >=2.0
This package is not auto-updated.
Last update: 2024-09-19 00:12:41 UTC
README
GoogleBundle可以向您的应用程序添加各种与Google相关的服务。这包括Google Analytics、Adwords和静态地图。
安装
初始化子模块
git submodule add git@github.com:antimattr/GoogleBundle.git src/AntiMattr/GoogleBundle
应用程序内核
将GoogleBundle添加到应用程序内核的registerBundles()方法中
public function registerBundles()
{
return array(
new AntiMattr\GoogleBundle\GoogleBundle(),
);
}
配置
Google Analytics
应用程序config.yml
通过在应用程序的config.yml文件中添加以下内容来启用Google Analytics服务的加载
google:
analytics:
trackers:
default:
name: MyJavaScriptCompatibleVariableNameWithNoSpaces
accountId: UA-xxxx-x
domain: .mydomain.com
trackPageLoadTime: true
视图
在head标签中或在你布局的</body>标签之前包含Google Analytics Async模板(模板将懒加载_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);
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 Maps - 静态地图
应用程序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 Maps
{% if google_maps.hasMaps() %}
{% for map in google_maps.getMaps() %}
{% autoescape false %}
{{ map.render }}
{% endautoescape %}
{% endfor %}
{% endif %}
Google Maps - JavaScript地图
应用程序config.yml
通过在应用程序的config.yml文件中添加以下内容来启用Google Maps服务的加载
google:
maps: ~
布局base.html.twig
添加JavaScript库
{% javascripts '@GoogleBundle/Resources/public/js/*'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
控制器
use AntiMattr\GoogleBundle\Maps\JavascriptMap;
use AntiMattr\GoogleBundle\Maps\Marker;
use AntiMattr\GoogleBundle\MapsManager;
...
$map = new JavascriptMap();
$map->setId('demo-map');
/**
* Add marker
*/
$marker = new Marker();
$marker->setLatitude(50.294492);
$marker->setLongitude(18.67138);
$marker->setMeta(array(
'infowindow' => "<div>Marker 1</div>"
));
$map->addMarker($marker);
// Fit map to markers
$map->setFitToMarkers(true);
$this->get('google.maps')->addMap($map);
视图
按照如下方式在你的模板中包含Google Maps
{% if google_maps.hasMaps() %}
{% for map in google_maps.getMaps() %}
{% if loop.first %}
{# Render GoogleMap link only once #}
<script type="text/javascript" src="{{ map.googleMapLibrary }}"></script>
{% endif %}
{% autoescape false %}
{{ map.render }}
{% endautoescape %}
{% endfor %}
{% endif %}