11var gaProxyApp = {
2- firstVisit: false,
32 clientId: '',
43 metricNameSpace: '',
4+ isFirstVisit: false,
55 trackingId: '',
66 clientIdCookieName: 'gap.clientId',
7- cookieDomain: '',
8- clientId: '',
97 serviceBaseUrl: '<%= serviceBaseUrl %> ',
108
119 collect: function (action, data) {
1210 data.trackingId = this.trackingId;
13- data.cookieDomain = this.cookieDomain;
1411 data.clientId = this.clientId;
1512 data.metricNameSpace = this.metricNameSpace;
1613
@@ -59,19 +56,19 @@ var gaProxyApp = {
5956 }
6057 },
6158
62- setCookie: function createCookie (name, value, days ) {
59+ setCookie: function (name, value, secondsExpire ) {
6360 var expires = '';
6461
65- if (days ) {
62+ if (secondsExpire ) {
6663 var date = new Date();
67- date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
64+ date.setTime(date.getTime() + (secondsExpire * 1000));
6865 expires = '; expires=' + date.toUTCString();
6966 }
7067
7168 document.cookie = name + '=' + value + expires + '; path=/';
7269 },
7370
74- readCookie: function readCookie (name) {
71+ readCookie: function (name) {
7572 var nameEQ = name + '=';
7673 var ca = document.cookie.split(';');
7774 for (var i = 0; i < ca.length; i++) {
@@ -82,68 +79,77 @@ var gaProxyApp = {
8279 return null;
8380 },
8481
85- createGuid: function guid () {
86- function s4() {
82+ createGuid: function () {
83+ function s4 () {
8784 return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
8885 }
86+
8987 return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
9088 },
9189
9290 checkRequired: function () {
93- if (!this.trackingId || !this.cookieDomain ) {
94- throw('Tracking ID and cookie domain was not set.');
91+ if (!this.trackingId) {
92+ throw('Tracking ID was not set.');
9593 }
9694
9795 return true;
9896 },
9997
100- inferClientId: function(trackingId) {
98+ inferFirstVisit: function () {
99+ return !this.readCookie(this.clientIdCookieName);
100+ },
101+
102+ inferClientId: function (cookieFields) {
103+ if (cookieFields.clientId) {
104+ return cookieFields.clientId;
105+ }
106+
101107 if (this.readCookie(this.clientIdCookieName)) {
102108 return this.readCookie(this.clientIdCookieName);
103109 }
104110
105- this.firstVisit = true;
106-
107- this.setCookie(
108- this.clientIdCookieName,
109- this.generateTrackingId(trackingId)
110- )
111+ return this.generateClientId();
111112 },
112113
113- generateTrackingId : function (trackingId ) {
114- if (trackingId ) {
115- return trackingId ;
114+ inferSecondsExpire : function (cookieFields ) {
115+ if (typeof cookieFields === 'object' ) {
116+ return cookieFields.cookieExpires ;
116117 }
118+ },
117119
120+ generateClientId: function () {
118121 return this.createGuid();
119122 },
120123
124+ init: function (element) {
125+ this.metricNameSpace = element[1];
126+ },
127+
121128 create: function (element) {
122129 if (!element[1]) {
123130 throw('Tracking ID was not specified');
124131 }
125132
126133 this.trackingId = element[1];
127134
128- if (!this.clientId) {
129- this.clientId = this.inferClientId();
130- }
135+ this.clientId = this.inferClientId(element[2]);
131136
132- this.cookieDomain = element[2];
137+ this.isFirstVisit = this.inferFirstVisit();
138+
139+ if (this.isFirstVisit) {
140+ this.setCookie(
141+ this.clientIdCookieName,
142+ this.clientId,
143+ this.inferSecondsExpire(element[2])
144+ );
145+ }
133146
134147 return {
135148 trackingId: element[1],
136- cookieDomain: element[2],
137- firstVisit: this.firstVisit
149+ firstVisit: this.isFirstVisit
138150 };
139151 },
140152
141- init: function (element) {
142- this.metricNameSpace = element[1];
143-
144- this.clientId = this.inferClientId(element[2]);
145- },
146-
147153 send: function (element) {
148154 this.checkRequired();
149155
@@ -196,7 +202,7 @@ var gaProxyApp = {
196202(function () {
197203 // Check if GAProxy object is set
198204 if (!window.gap) {
199- console.error('GAProxy object is not defined.');
205+ console.error('GA proxy object is not defined.');
200206 return false;
201207 }
202208
0 commit comments