README 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. = sfGoogleAnalyticsPlugin plugin =
  2. Easily add [http://www.google.com/analytics Google Analytics] tracking code to your presentation layer.
  3. ''This documentation is a work in progress. Thank you for your patience.''
  4. == Installation ==
  5. === 1. Install ===
  6. You can install using the `plugin-install` task:
  7. {{{
  8. php symfony plugin-install sfGoogleAnalyticsPlugin
  9. }}}
  10. You can also pull the code directly from the [http://svn.symfony-project.org/plugins/sfGoogleAnalyticsPlugin/trunk Subversion repository] using a `svn checkout` or the `svn:externals` property on your project's `/plugins` directory.
  11. Once the plugin code is accessible to your project, you need to add the `sfGoogleAnalyticsFilter` to your filter chain:
  12. {{{
  13. #!yaml
  14. rendering: ~
  15. security: ~
  16. # insert your own filters here
  17. sf_google_analytics_plugin:
  18. class: sfGoogleAnalyticsFilter
  19. cache: ~
  20. common: ~
  21. execution: ~
  22. }}}
  23. ''NOTE: This is the symfony 1.1 `filters.yml` file. The equivalent symfony 1.0 file looks slightly different.''
  24. === 2. Configure ===
  25. Basic configuration is done in your application's `app.yml` file:
  26. {{{
  27. #!yaml
  28. all:
  29. sf_google_analytics_plugin:
  30. enabled: on
  31. profile_id: XX-XXXXX-X
  32. tracker: google
  33. }}}
  34. You'll have to copy the `profile_id` value out of the tracking code Google supplies for your site profile. This value typically starts with the letter U and ends with a single digit.
  35. This plugin defaults to using the older `urchin` tracker. To take advantage of the latest featureset of Google Analytics, change the `tracker` value to `google` for the older snippet or `asynchronous` for the more performant one. This will insert the new `ga.js` tracking code into your project.
  36. == Advanced Usage ==
  37. This plugin provides much more functionality than a simple insert of your tracking code. Here are some highlights:
  38. === How do I customize the name a page is tracked as? ===
  39. If you would like to track a certain page as something other than what appears in the browser address bar, you can do so by modifying the `page_name` parameter in `module.yml`:
  40. {{{
  41. #!yaml
  42. all:
  43. myAction:
  44. sf_google_analytics_plugin:
  45. params:
  46. page_name: something_else
  47. }}}
  48. For finer control over when the alternate page name is used, you can access the tracker object directly in your action. This also exposes additional funcionality.
  49. ==== Option: `use_flash` ====
  50. For example, if you want to track a successful form submission for a form that redirects to the same page on success and on error:
  51. {{{
  52. #!php
  53. <?php
  54. class mainActions extends sfActions
  55. {
  56. public function executeContact()
  57. {
  58. // form submission logic...
  59. if ($success)
  60. {
  61. $this->getTracker()->setPageName('/contact/success', array(
  62. 'use_flash' => true,
  63. ));
  64. $this->setFlash('feedback', 'Thank you!');
  65. $this->redirect('main/contact');
  66. }
  67. }
  68. }
  69. }}}
  70. In this example, the request after the successful form post will be tracked as `/contact/success`.
  71. ==== Option: `is_route` ====
  72. One more option available is the `is_route` option. When this flag is applied, the string provided for a page name will be passed through `sfRouting` before being added to the page. Using this option allows you to centralize all URLs, those real and for tracking purposes only, in your application's `routing.yml` file:
  73. {{{
  74. #!yaml
  75. contact:
  76. url: /contact
  77. param: { module: main, action: contact }
  78. # be sure the tracking rule comes AFTER the real rule so the application
  79. # doesn't use it for any url_for('main/contact') calls
  80. track_contact:
  81. url: /contact/success
  82. param: { module: main, action: contact }
  83. }}}
  84. {{{
  85. #!php
  86. <?php
  87. class mainActions extends sfActions
  88. {
  89. public function executeContact()
  90. {
  91. // form submission logic...
  92. if ($success)
  93. {
  94. $this->getTracker()->setPageName('@track_contact', array(
  95. 'use_flash' => true,
  96. 'is_route' => true,
  97. ));
  98. $this->setFlash('feedback', 'Thank you!');
  99. $this->redirect('@contact');
  100. }
  101. }
  102. }
  103. }}}
  104. === How do I selectively disable tracking? ===
  105. You can easily configure the tracking code for a single module or even a single action by using the `module.yml` configuration file:
  106. {{{
  107. #!yaml
  108. all:
  109. # disable tracking for this module...
  110. sf_google_analytics_plugin:
  111. params:
  112. enabled off
  113. # ...or for a single action
  114. index:
  115. sf_google_analytics_plugin:
  116. params:
  117. enabled off
  118. }}}
  119. Alternatively, you can access the tracker object directly from inside your action:
  120. {{{
  121. #!php
  122. <?php
  123. class mainActions extends sfActions
  124. {
  125. public function executeIndex()
  126. {
  127. $this->getTracker()->setEnabled(false);
  128. }
  129. }
  130. }}}
  131. === Can I insert the tracking code at the top of my page? ===
  132. You can configure this in `app.yml`:
  133. {{{
  134. #!yaml
  135. all:
  136. sf_google_analytics_plugin:
  137. profile_id: XX-XXXXX-X
  138. insertion: top
  139. }}}
  140. === Can I track demographic information? ===
  141. You can expose whatever information you store on your users (that your privacy policy allows, of course) to Google Analytics. This is best done in your sign-in routine. For example, if you're using [wiki:sfGuardPlugin]:
  142. {{{
  143. #!php
  144. <?php
  145. class myUser extends sfGuardSecurityUser
  146. {
  147. /**
  148. * Overload to add custom tracking variables to the current user.
  149. *
  150. * Variables are added using flash, assuming sign in will be followed by a
  151. * redirect.
  152. *
  153. * @see sfGuardSecurityUser
  154. */
  155. public function signIn($user, $remember = false, $con = null)
  156. {
  157. parent::signIn($user, $remember, $con);
  158. // assign tracking variables
  159. if ($gender = $user->getProfile()->getGender())
  160. {
  161. $this->getTracker()->setVar('gender/'.$gender, array(
  162. 'use_flash' => true,
  163. ));
  164. }
  165. if ($this->hasCredential('moderator'))
  166. {
  167. $this->getTracker()->setVar('userType/moderator', array(
  168. 'use_flash' => true,
  169. ));
  170. }
  171. }
  172. }
  173. }}}
  174. == Changelog ==
  175. === Version 1.1.2 ===
  176. * Fixed symfony 1.1 compatibility in `sfLogger` interactions.
  177. === Version 1.1.1 ===
  178. * Fixed Javascript case-sensitivity bug.
  179. === Version 1.1.0 ===
  180. * '''Added support for new Google Javascript library (`ga.js`).'''
  181. * Updated API to include more human-readable method names.
  182. * Added support for tracking e-commerce transactions.
  183. * Added option to parse tracking argument with `sfRouting`.
  184. * Added option to defer many tracker calls to the next response, similar to `sfFlash` storage (helpful for redirects).
  185. === Version 1.0-RC1 ===
  186. * Renamed plugin from sfUrchinPlugin to sfGoogleAnalyticsPlugin.
  187. === Version 0.3.1-beta ===
  188. * Bugfix to insertion top to accommodate `<body>` tags with attributes.
  189. === Version 0.3.0-beta ===
  190. * Broke filter logic into protected methods for easy overloading.
  191. * Added insertion configuration.
  192. === Version 0.2.0-beta ===
  193. * Added support for SSL requests.
  194. * Added mixin methods to actions for easy modification of initialization variables and parameters.
  195. * Added escaping of Javascript values.
  196. === Version 0.1.0-beta ===
  197. * Initial public release.
  198. == Maintainers ==
  199. Kris Wallsmith