Wondering why you can't post?
To post, head over to our current forums.
   
 
Plugin: Google Analytics
Posted: 27 September 2009 02:43 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

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} 
File Attachments
pi.google_analytics.0.2.php.zip  (File Size: 3KB - Downloads: 183)
Profile
 
 
Posted: 27 September 2009 06:51 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  19056
Joined  04-29-2002

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.

 Signature 

EE 2.10 - 2.x Bug Tracker | Docs | Upgrade from 1.x to 2.x | Build Update Instructions | 1.6.x Update Info

Most recent 2.x version: Build 20100810
Don’t forget to upgrade MSM and Forum as needed!

Profile
MSG
 
 
Posted: 27 September 2009 08:25 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

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

Profile
 
 
Posted: 29 September 2009 05:23 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  186
Joined  01-20-2006

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

 Signature 

buro taggetig: web design & event photography from Belgium.  |  Follow me on Twitter

Profile
 
 
Posted: 29 September 2009 08:55 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

... or simply write the GA code in template(s)? All very good strategies, true smile. 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 wink

Profile
 
 
Posted: 02 October 2009 03:34 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  283
Joined  05-07-2006

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:

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{} 

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.

 Signature 

The Watchmaker Project | My EE add-ons | Follow me on Twitter

Profile
 
 
Posted: 02 October 2009 04:45 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

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.

Profile
 
 
Posted: 06 October 2009 09:00 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

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.

Profile
 
 
Posted: 15 October 2009 02:50 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  44
Joined  05-02-2008

useful thanks, using it on my site.

Profile
 
 
Posted: 27 March 2010 03:48 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  03-24-2007

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.

File Attachments
pi_google_analytics_0.4.zip  (File Size: 5KB - Downloads: 63)
 Signature 

Twitter | Facturation en ligne avec iScriba (one of my CodeIgniter apps)

Profile
 
 
Posted: 15 April 2010 08:11 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-30-2008

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?

Profile
 
 
Posted: 15 April 2010 08:19 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  03-24-2007

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 wink

 Signature 

Twitter | Facturation en ligne avec iScriba (one of my CodeIgniter apps)

Profile
 
 
   
 
 
‹‹ imgsizer Issue      Plugin: Search Fields ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 149967 Total Logged-in Users: 84
Total Topics: 103610 Total Anonymous Users: 35
Total Replies: 518110 Total Guests: 294
Total Posts: 621720    
Members ( View Memberlist )