@@ -21,12 +21,50 @@ var PiwikTracker = function(opts) {
2121 return process && process . env && process . env . NODE_ENV ? process . env . NODE_ENV . toLowerCase ( ) : 'development' ;
2222 } ;
2323
24+ var getBaseUrl = function ( ) {
25+ var u = ''
26+ var url = opts . url || window . location . hostname ;
27+
28+ if ( url . indexOf ( 'http://' ) !== - 1 || url . indexOf ( 'https://' ) !== - 1 ) {
29+ u = url + '/' ;
30+ }
31+ else {
32+ u = ( ( 'https:' == document . location . protocol ) ? 'https://' + url + '/' : 'http://' + url + '/' ) ;
33+ }
34+
35+ return u ;
36+ }
37+
2438 var piwikIsAlreadyInitialized = function ( ) {
2539 var scripts = document . getElementsByTagName ( 'script' ) ;
40+ var scriptUrl = getBaseUrl ( ) + opts . clientTrackerName ;
41+
42+ var found = false ;
2643
2744 for ( var i = 0 , n = scripts . length ; i < n ; i ++ ) {
28- if ( scripts [ i ] . getAttribute ( 'data-' + opts . piwikScriptDataAttribute ) !== null ) {
29- return true ;
45+ if ( scripts [ i ] . getAttribute ( 'src' ) === scriptUrl ) {
46+ found = true ;
47+ break ;
48+ }
49+ }
50+
51+ if ( ! found ) {
52+ return false ;
53+ }
54+
55+ var hasSiteId = false ;
56+ var hasTrackerUrl = false ;
57+ for ( var j = 0 , l = window . _paq . length ; j < l ; j ++ ) {
58+ if ( ~ window . _paq [ j ] . indexOf ( 'setSiteId' ) ) {
59+ hasSiteId = true
60+ }
61+
62+ if ( ~ window . _paq [ j ] . indexOf ( 'setTrackerUrl' ) ) {
63+ hasTrackerUrl = true
64+ }
65+
66+ if ( hasTrackerUrl && hasSiteId ) {
67+ return true
3068 }
3169 }
3270
@@ -44,7 +82,15 @@ var PiwikTracker = function(opts) {
4482 opts . serverTrackerName = ( ( opts . serverTrackerName !== undefined ) ? opts . serverTrackerName : 'piwik.php' ) ;
4583 opts . piwikScriptDataAttribute = ( ( opts . piwikScriptDataAttribute !== undefined ) ? opts . piwikScriptDataAttribute : 'piwik-react-router' ) ;
4684
47- if ( ( ! opts . url || ! opts . siteId ) && ! piwikIsAlreadyInitialized ( ) ) {
85+ window . _paq = window [ '_paq' ] || [ ] ;
86+
87+
88+ var alreadyInitialized = piwikIsAlreadyInitialized ( ) ;
89+ var piwikWithoutUrlOrSiteId = ( ! opts . url || ! opts . siteId ) && ! alreadyInitialized ;
90+ var piwikWithoutInjectScript = ! opts . injectScript && ! alreadyInitialized ;
91+
92+
93+ if ( piwikWithoutUrlOrSiteId || piwikWithoutInjectScript ) {
4894 // Only return warning if this is not in the test environment as it can break the Tests/CI.
4995 if ( getEnvironment ( ) !== 'test' ) {
5096 warning ( null , 'PiwikTracker cannot be initialized! You haven\'t passed a url and siteId to it.' ) ;
@@ -53,8 +99,6 @@ var PiwikTracker = function(opts) {
5399 return apiShim ;
54100 }
55101
56- window . _paq = window [ '_paq' ] || [ ] ;
57-
58102 /**
59103 * Adds a page view for the given location
60104 */
@@ -165,11 +209,7 @@ var PiwikTracker = function(opts) {
165209 var alreadyInitialized = piwikIsAlreadyInitialized ( ) ;
166210
167211 if ( ! alreadyInitialized ) {
168- if ( opts . url . indexOf ( 'http://' ) !== - 1 || opts . url . indexOf ( 'https://' ) !== - 1 ) {
169- var u = opts . url + '/' ;
170- } else {
171- var u = ( ( 'https:' == document . location . protocol ) ? 'https://' + opts . url + '/' : 'http://' + opts . url + '/' ) ;
172- }
212+ var u = getBaseUrl ( )
173213
174214 push ( [ 'setSiteId' , opts . siteId ] ) ;
175215 push ( [ 'setTrackerUrl' , u + opts . serverTrackerName ] ) ;
0 commit comments