Jump to:
| Project: | Google Analytics |
| Version: | 7.x-1.2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
| Issue tags: | #d7ux |
Issue Summary
I'm a typical use case. I'd like to add some Google Analytics goal tracking to my Drupal site, but I'm lazy. Adding them manually to end points is quite time consuming. There would also be problems with making sure these end points don't get triggered twice for the same action.
Solution: Integrate Google Analytic module with Rules.
Rules gives us a lot of wonderful "Events" which are useful for Google Analytics Goal tracking. The best thing about doing it this way, is you'll be able to integrate Google Analytics goals very easily in the future for any module which integrates it's workflow with Rules.
Here are some events which could be used (there are tonnes more)
- Ubercart - Customer Completes Checkout
- Comments - After Saving a new comment
- Node - After saving new content
- User - After saving a new user account
- User - User has logged in
For me at least, I'd personally want all these tracked in Google Analytics as goals. I think it would be a very useful benchmark of how my site (and changes) are doing.
As for implementation, I would assume Google Analytics module after these rules are triggered, could simply inject some goal javascript into the next page request and make sure it only happens once per event.
We would also need to make sure this works with Anonymous page caching.
So developers. Is this feasible?
Please +1 to show your support for this.
Additional Bikeshed (not to be accomplished on first iteration, but version #2): What about adding Goal funnels for tracking abandon rate. Can we do this currently with Rules, or will we have to create some expanded rules to make this happen. Something to think about.
Here's some reading on Google Analytics Event Tracking with Goals:
http://analytics.blogspot.com/2011/04/new-google-analytics-events-goals....
Comments
#1
I've got this working with my Drupal 7 install with Rules.
Event -> new user is saved
Action -> PHP Code
<?php
// Comment out, violates google analytics TOS
//$_SESSION['ga_push'][] = array('_trackEvent', 'user', 'register', 'UserID', $account->uid);
// This is fine
$_SESSION['ga_push'][] = array('_trackEvent', 'user', 'register');
?>
Then I added some code in a theme hook
<?phpfunction theopen_preprocess_html(&$variables) {
$variables['ga_push'] = '';
if(!empty($_SESSION['ga_push'])) {
$values = $_SESSION['ga_push'];
foreach($values as $i => $value) {
foreach($value as $k => $ga_item) {
$values[$i][$k] = "'" . $ga_item. "'";
}
$variables['ga_push'] .= '_gaq.push([' . implode(',', $values[$i]) . ']); ';
}
$variables['ga_push'] = '<script type="text/javascript">' . $variables['ga_push'] . '</script>';
unset($_SESSION['ga_push']);
}
}
?>
Then I added this into my html.tpl.php just above </body>
<?phpprint $ga_push;
?>
Ideally this would be abstracted a little better and a nice UI done with Rules....but it appears to be working as I want.
Would be very simple to add, and you should/could include some default rules as examples. User Register & New Content Posted are good ones. Then users could edit them, or create others. I'm sure after some time, please would create some wonderful ones, which would greatly add value to the Google Analytics module.
#2
This is a pretty good idea, would be nice if someone could implement it.
#3
Looks like a good solution. Where does the new variable go in relation to the existing analytics code generated by the module?
#4
Ideally someone would make a Rules "action" for GA _trackEvent which would let you fill out the variables for it's arguments. Then you could just add this "action" to existing "events" and then you'd have some event tracking in your GA.
I've enabled this for one of my sites. I've re-worked my code from what I've posted as I don't think I was using the arguments right. Hopefully this site will launch in beta very soon and I'll start collecting some stats and refine how it should all work...but that's the general idea.
It would help a tonne of site administrators gather a lot of wonderful information about abandon rates if this idea was expanded on. This in turn could provide a huge amount to of UX statistics in analysis of Drupal as a whole, as it would be easy enough for everyone to enable and start gathering data. This is also one of the reasons I would recommend implementing these rules by default. In turn modules would see what's going on over here and hook into the GA module and providing reasonable entry points where this type of information could/should be collected...which could then easily be enabled by everyone.
I'm personally very excited about this and I think it could be quite useful and important.
#5
jOrd: you are NOT allowed to track user identifyable data (userid) with google analytics!!! I suggest you to read the google rules again. Code above have critical security issues. Don't use this code snippets.
#231451: Add hook_google_analytics for other modules need to be done first.
#6
Code above has privacy issues, but not security issues. Here's the snippet from GA TOS . Let's factor that in while creating a solution.
7. PRIVACY . You will not (and will not allow any third party to) use the Service to track or collect personally identifiable information of Internet users, nor will You (or will You allow any third party to) associate any data gathered from Your website(s) (or such third parties' website(s)) with any personally identifying information from any source as part of Your use (or such third parties' use) of the Service. You will have and abide by an appropriate privacy policy and will comply with all applicable laws relating to the collection of information from visitors to Your websites. You must post a privacy policy and that policy must provide notice of your use of a cookie that collects anonymous traffic data.More importantly as I previously mentioned, my code is wrong and tracking the value of the UserID in the value of the label, is the wrong way to do it. I was mostly interested in tracking how many signups, so I've changed my "event" code to this.
<?php$_SESSION['ga_push'][] = array('_trackEvent', 'user', 'register');
?>
Either way, my idea still stands and I feel is more useful than a simple hook. Also implementing both would cause fragmentation in how people implement integration. Additionally that hook_google_analytics feature request is 3 years old and according to you won't get implemented because of "design flaws".
The downfall of this suggested solution, is that it requires the rules and requires a 3rd party module to integrate with rules before it could use analytics goal tracking. This is a higher burden of entry than a simple hook.
The good thing about it, is that it auto-magically integrates with any module already using rules (like much of core and all current eCommerce platforms on Drupal). It would have the added by-product of making other modules integrate with rules so they can accept Google Analytics tracking.
@hass with that said what would you need to see to get this implemented an eventTracking feature in to Google Analytics module? I'm willing to do the work, but I'm not willing to do it if you're not going to integrate it. So let me know what you need to consider something like this (with hooks or rules or both) and I'll do the work.
#7
Code injection is possible. There is a security issue.
#8
@hass please elaborate on the vunerability so i can resolve it.
Additionally please tell me what work would need to get done to add eventTracking in to the Google Analytics module.
#9
Look into the api docs for drupal_to_js, drupal_json and drupal_add_js and as i know there is an api for session variables. Security issue is at $ga_item and implode. The linked issue should be fixed first and than we can look forward about the best way to get this stuff in.
#10
subscribe
#11
#231451: Add hook_google_analytics for other modules
#12
Its a dupe of which issue? So i can follow along.
#13
@j0rd how did you end up accomplishing goal tracking? I imagine there are about 50,000 others that would benefit from this feature request, or a how to. Thanks!
#14
@castelar . I haven't implemented it yet, other than the way which @hass said is a security problem.
This is still a feature I think a lot of people could benefit from as well, and I'd still like to do it. I have a project coming up which integrates with Rules pretty deeply and I might be able to get them to pay me to do it. If that's the case I'll make sure it's robust and useful for others.
Either way, competing implementation could helps everyone come up with the best / ideal solution, so if you want this feature please implement it as well and we can see who does it best.
#15
This is not a duplicate of #231451: Add hook_google_analytics for other modules, which is about adding a new hook. This is about adding Rules integration - completely different. Mark it as won't fix if you like, but dont mark it as a duplicate as it clearly isn't.
#16
Yes, but the integration of an external module that uses rules to enhance the tracker code need the hook, isn't it? :-) Therefore was my idea to push getting the hook in to allow other modules to enhance the tracker. I'm not sure what you may plan to track based on rules, but I'm open minded to new ideas and *patches* for the ga module.
#17
I have since found GA Push which adds the ability to trigger custom GA Events to be fired based on Rules. It works very well and was exactly what I was looking for. Over and above triggering Events with Rules, I'm not what else it would be useful to have Rules do. But the fact that GA Push now provides this feature, I'd say this issue could possibly just be closed now.
#18
@mrfelton
The idea behind integrating Rules & GA is that out of the box (at least in D6), Rules gives you these events. Almost every single one would be handy to have tracked in GA.
Additionally other modules integrate with Rules (Drupal Commerce as an example) and will then provide additional entry points where you can easily add new tracking.
Curious if this is possible with GA Push, and how much work is it to implement. Is it something a non-technical user could figure out?
Comment
After deleting a comment
After publishing a comment
After saving a new comment
After saving an updated comment
After unpublishing a comment
Comment is being viewed
Node
After deleting content
After saving new content
After updating existing content
Content is going to be saved
Content is going to be viewed
System
Cron maintenance tasks are performed
User is going to view a page
Taxonomy
After saving a new term
After updating a term
User
User account details have been updated
User account has been created
User has been deleted
User has logged in
User has logged out
User page has been viewed
#19
There are unique urls. Login/logout are well known. Some others, too. Additionally many (most?) logged in users are not tracked if they are in admin sections.
#20
GA Push is very simple. It provides a new Rules Action that is able to trigger a GA Event to be registered. So, you can hook as mank of these as you want into as many Rules Events as you like. Essentially, gives you all the power of Rules Actions and Conditions to trigger GA Events.
#21
I hope these developer also blocks the use of personal data tokens.
I will review this module and may take over all this stuff.