source: Dev/trunk/src/client/dojox/analytics/Urchin.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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        lang.mixin(config,{
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:
17                //              A Google-analytics helper, for post-onLoad inclusion of the tracker, and
18                //              dynamic tracking during long-lived page cycles.
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                // example:
36                //      |       // create the tracker programatically:
37                //      |       var tracker = new dojox.analytics.Urchin({ acct:"UA-123456-7" });
38                //
39                // example:
40                //      |       // define the urchin djConfig option:
41                //      |       var djConfig = { urchin: "UA-123456-7" };
42                //      |
43                //      |       // and in markup:
44                //      |       <div dojoType="dojox.analytics.Urchin"></div>
45                //      |       // or code:
46                //      |       new dojox.analytics.Urchin();
47                //
48                // example:
49                //      |       // create and define all analytics with one tag.
50                //      |       <div dojoType="dojox.analytics.Urchin" acct="UA-12345-67"></div>
51
52                // acct: String
53                //              your GA urchin tracker account number. Overrides `djConfig.urchin`
54                acct: "",
55
56                constructor: function(args){
57                        // summary:
58                        //              Initialize this Urchin instance. Immediately starts the load
59                        //              sequence, so defer construction until (ideally) after onLoad and
60                        //              potentially widget parsing.
61                        this.tracker = null;
62                        lang.mixin(this, args);
63                        this.acct = this.acct || config.urchin;
64
65                        var re = /loaded|complete/,
66                                gaHost = ("https:" == window.doc.location.protocol) ? "https://ssl." : "http://www.",
67                                h = window.doc.getElementsByTagName("head")[0],
68                                n = construct.create('script', {
69                                        src: gaHost + "google-analytics.com/ga.js"
70                                }, h);
71
72                        n.onload = n.onreadystatechange = lang.hitch(this, function(e){
73                                if(e && e.type == "load" || re.test(n.readyState)){
74                                        n.onload = n.onreadystatechange = null;
75                                        this._gotGA();
76                                        h.removeChild(n);
77                                }
78                        });
79
80                },
81
82                _gotGA: function(){
83                        // summary:
84                        //              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:
115                        //              A public API attached to this widget instance, allowing you
116                        //              Ajax-like notification of updates.
117                        // url: String
118                        //              A location to tell the tracker to track, eg: "/my-ajaxy-endpoint"
119                        // example:
120                        //              Track clicks from a container of anchors and populate a `ContentPane`
121                        //      |       // 'tracker' is our `Urchin` instance, pane is the `ContentPane` ref.
122                        //      |       dojo.connect(container, "onclick", function(e){
123                        //      |               var ref = dojo.attr(e.target, "href");
124                        //      |               tracker.trackPageView(ref);
125                        //      |               pane.attr("href", ref);
126                        //      |       });
127                       
128                        this.tracker._trackPageview.apply(this.tracker, arguments);
129                }
130
131        });
132});
Note: See TracBrowser for help on using the repository browser.