source: Dev/branches/rest-dojo-ui/client/dojox/analytics/Urchin.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.6 KB
Line 
1define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/window",
2        "dojo/_base/config", "dojo/dom-construct"
3], function(lang, declare, window, config, construct){
4
5        /*=====
6        dojo.mixin(djConfig,{
7                // urchin: String
8                //              Used by `dojox.analytics.Urchin` as the default UA-123456-7 account
9                //              number used when being created. Alternately, you can pass an acct:""
10                //              parameter to the constructor a la: new dojox.analytics.Urchin({ acct:"UA-123456-7" });
11                urchin: ""
12        });
13        =====*/
14
15        return declare("dojox.analytics.Urchin", null, {
16                // summary: A Google-analytics helper, for post-onLoad inclusion of the tracker, and
17                //              dynamic tracking during long-lived page cycles.
18                //
19                // description:
20                //              A small class object will allows for lazy-loading the Google Analytics API
21                //              at any point during a page lifecycle. Most commonly, Google-Analytics is loaded
22                //              via a synchronous script tag in the body, which causes `dojo.addOnLoad` to
23                //              stall until the external API has been completely loaded. The Urchin helper
24                //              will load the API on the fly, and provide a convenient API to use, wrapping
25                //              Analytics for Ajaxy or single page applications.
26                //
27                //              The class can be instantiated two ways: Programatically, by passing an
28                //              `acct:` parameter, or via Markup / dojoType and defining a djConfig
29                //              parameter `urchin:`
30                //
31                //              IMPORTANT:
32                //              This module will not work simultaneously with the core dojox.analytics
33                //              package. If you need the ability to run Google Analytics AND your own local
34                //              analytics system, you MUST include dojox.analytics._base BEFORE dojox.analytics.Urchin
35                //
36                //      example:
37                //      |       // create the tracker programatically:
38                //      |       var tracker = new dojox.analytics.Urchin({ acct:"UA-123456-7" });
39                //
40                //      example:
41                //      |       // define the urchin djConfig option:
42                //      |       var djConfig = { urchin: "UA-123456-7" };
43                //      |
44                //      |       // and in markup:
45                //      |       <div dojoType="dojox.analytics.Urchin"></div>
46                //      |       // or code:
47                //      |       new dojox.analytics.Urchin();
48                //
49                //      example:
50                //      |       // create and define all analytics with one tag.
51                //      |       <div dojoType="dojox.analytics.Urchin" acct="UA-12345-67"></div>
52                //
53                // acct: String
54                //              your GA urchin tracker account number. Overrides `djConfig.urchin`
55                acct: "",
56
57                constructor: function(args){
58                        // summary:
59                        //              Initialize this Urchin instance. Immediately starts the load
60                        //              sequence, so defer construction until (ideally) after onLoad and
61                        //              potentially widget parsing.
62                        this.tracker = null;
63                        lang.mixin(this, args);
64                        this.acct = this.acct || config.urchin;
65
66                        var re = /loaded|complete/,
67                                gaHost = ("https:" == window.doc.location.protocol) ? "https://ssl." : "http://www.",
68                                h = window.doc.getElementsByTagName("head")[0],
69                                n = construct.create('script', {
70                                        src: gaHost + "google-analytics.com/ga.js"
71                                }, h);
72
73                        n.onload = n.onreadystatechange = lang.hitch(this, function(e){
74                                if(e && e.type == "load" || re.test(n.readyState)){
75                                        n.onload = n.onreadystatechange = null;
76                                        this._gotGA();
77                                        h.removeChild(n);
78                                }
79                        });
80
81                },
82
83                _gotGA: function(){
84                        // summary: initialize the tracker
85                        this.tracker = _gat._getTracker(this.acct);
86                        this.GAonLoad.apply(this, arguments);
87                },
88
89                GAonLoad: function(){
90                        // summary:
91                        //              Stub function to fire when urchin is complete
92                        //      description:
93                        //              This function is executed when the tracker variable is
94                        //              complete and initialized. The initial trackPageView (with
95                        //              no arguments) is called here as well, so remeber to call
96                        //              manually if overloading this method.
97                        //
98                        //      example:
99                        //      Create an Urchin tracker that will track a specific page on init
100                        //      after page load (or parsing, if parseOnLoad is true)
101                        //      |       dojo.addOnLoad(function(){
102                        //      |               new dojox.ananlytics.Urchin({
103                        //      |                       acct:"UA-12345-67",
104                        //      |                       GAonLoad: function(){
105                        //      |                               this.trackPageView("/custom-page");
106                        //      |                       }
107                        //      |               });
108                        //      |       });
109                       
110                        this.trackPageView();
111                },
112
113                trackPageView: function(/* string */url){
114                        // summary: A public API attached to this widget instance, allowing you
115                        //              Ajax-like notification of updates.
116                        //
117                        //      url: String
118                        //              A location to tell the tracker to track, eg: "/my-ajaxy-endpoint"
119                        //
120                        //      example:
121                        //      Track clicks from a container of anchors and populate a `ContentPane`
122                        //      |       // 'tracker' is our `Urchin` instance, pane is the `ContentPane` ref.
123                        //      |       dojo.connect(container, "onclick", function(e){
124                        //      |               var ref = dojo.attr(e.target, "href");
125                        //      |               tracker.trackPageView(ref);
126                        //      |               pane.attr("href", ref);
127                        //      |       });
128                       
129                        this.tracker._trackPageview.apply(this, arguments);
130                }
131
132        });
133});
Note: See TracBrowser for help on using the repository browser.