Haven’t seen a Google analytics plugin anywhere, so I created this simple plugin, which prints out the Google Analytics code needed to track website statistics.
Basically I was tired of hunting down for that short but complicated GA code on the web every time I needed to install the tracking script. Was also tired of always having to remember the Web property ID, and having to paste it at the right place in the GA code.
Usage:
{exp:google_analytics id="UA-9999999-1"}or if you added your Web property ID in your config.php file as so:
$conf['google_analytics_id'] = "UA-9999999-1";… you can simply type
{exp:google_analytics}Parameters
id="UA-9999999-1"The Google Analytics ID for your site* * If you added your Web property ID in your config.php file, the id parameter is not needed and is ignored in the template.
code="urchin"If set to “urchin”, the old Google Analytics urchin code will be loaded. If set to “ga” or “normal”, or if the parameter is absent, the standard Google Analytics code will be loaded.
Useful for quickly adding the GA code to specific templates. Also useful with conditionals to turn off tracking for certain logged in members. For example, the following would turn off Google Analytics tracking when Super Admins are logged in:
{if logged_in && member_group != "1"}
{exp:google_analytics}
{/if}I don’t think there’s anything wrong with automating this kind of thing into a plugin.
One thing that this code misses, though, is the ability to accurately track site search. EE doesn’t use GET for the search parameter, so if you want to use Google Analytics to track your site searches you need to make some slight changes to the tracking call on search result pages.
Here’s how to do it in a regular template - I’ll leave it to Nicolas to figure out how to incorporate this into his plugin:
<pre><code>try { var pageTracker = _gat._getTracker(“UA-XXXXXX-1”); pageTracker._initData(); terms = ‘{exp:search:keywords}’; searchterms = terms.toLowerCase().replace(/\s/g,’+’); pageTracker._trackPageview(‘/search?q=’ + searchterms); } catch(err) {}</code></pre>
Obviously you’d need to wrap this in an if-else to check for the presence of the search keywords; this code will record a pageview to “/search” with your search term as the ‘q’ parameter. You can configure Site Search under the Profile Settings for your GA account.
I have updated the plugin to v0.2 to include site search tracking, as well as mailto/file download/outbound link tracking.
Changes: * New: Added search query tracking. Turned on by default and automatically triggered if on search result page * New: Added the {exp:google_analytics:track} tag, to track mailto/file download/outbound links
The plugin will track site searches by default when on a search result page (i.e. those pages with crazy search ids as the last segment). For flexibility, you can control site search tracking with the following parameters:
search_tracking="off"If set to “on” or if the parameter is absent, the plugin will track site search queries on search result pages, i.e. search result pages where the last segment is the EE search id. Set to anything else to turn off.
search_query_parameter="/search/results?query="Sets the search query parameter, set in Google Analytics. If the parameter is absent and search tracking is on, default is “/search?q=”.
Also added a quick tag to track mailto/file download/outbound links. Simply add onclick=”{exp:google_analytics:track link="/file/download/to/track/file.zip"}” to your links. Parameter:
link="/downloads/file.zip"The mailto/file/outbound link to track.
The plugin seems to work fine on my side (queries and file downloads are recorded in GA), but let me know how it goes.
Hi guys,
Hope Nicolas will not mind but I’ve taken the liberty to convert the plugin so it works under ExpressionEngine 2.0. I have also added some new functionnalities.
Changelog :
Version 0.4
- Changed some parameters name to reflect Google Analytics documentation
- Added the ability to set domain name
- Added the ability to set allow hash
- Added the ability to set allow linker
- Added the ability to load the asynchronous tracking code
- Cleaned up the code
Version 0.3
- ExpressionEngine 2.0 compatibility
- Cleaned up the codeYou’ll find it attached to this post. Enjoy !
PS : I did not add e-commerce tracking because I think it’s out of the scope of this plugin and also because it would probably require a module.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.