We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Google Analytics

Development and Programming

Nicolas Bottari's avatar
Nicolas Bottari
143 posts
16 years ago
Nicolas Bottari's avatar Nicolas Bottari

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}
       
Sue Crocker's avatar
Sue Crocker
26,054 posts
16 years ago
Sue Crocker's avatar Sue Crocker

Hi, Nicholas. Thanks for the plugin. Just wanted you to know that you could just use:

{if member_group != "1"}
{exp:google_analytics}
{/if}

You don’t need to check for both. Checking for the superadmin group is enough.

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
16 years ago
Nicolas Bottari's avatar Nicolas Bottari

Thank you, Sue! I didn’t know member_group only was enough in conditionals.

       
bobh's avatar
bobh
145 posts
16 years ago
bobh's avatar bobh

Seems a bit overkill to me thisone. Why not just put the ga code in an embed or -even better- a global variable?

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
16 years ago
Nicolas Bottari's avatar Nicolas Bottari

… or simply write the GA code in template(s)? All very good strategies, true 😊. However, no need to hunt down the GA code using the plugin. I simply wrote it for convenience, even though I might only be saving a step or two compared to other strategies 😉

       
Matthew Pennell's avatar
Matthew Pennell
221 posts
16 years ago
Matthew Pennell's avatar Matthew Pennell

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.

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
16 years ago
Nicolas Bottari's avatar Nicolas Bottari

Thank you Matthew. That’s a good idea, I’ll try to add this to the plugin, along with some other small features I had in mind.

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
16 years ago
Nicolas Bottari's avatar Nicolas Bottari

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.

       
Oxygen Solutions's avatar
Oxygen Solutions
40 posts
16 years ago
Oxygen Solutions's avatar Oxygen Solutions

useful thanks, using it on my site.

       
Matthieu Fauveau's avatar
Matthieu Fauveau
106 posts
16 years ago
Matthieu Fauveau's avatar Matthieu Fauveau

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 code

You’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.

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
about 16 years ago
Nicolas Bottari's avatar Nicolas Bottari

Merci Matthieu for porting the plugin. Great additions as well (wasn’t aware of some of those GA features!). Don’t mind if I put this up on the developer’s (me) site as well? Also don’t mind if I do a backport for EE 1.6.8?

       
Matthieu Fauveau's avatar
Matthieu Fauveau
106 posts
about 16 years ago
Matthieu Fauveau's avatar Matthieu Fauveau

Well that was just me playing with your plugin and learning EE plugins. I don’t mind if you follow up on this of course. It’s your plugin after all 😉

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.