Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piwik_log() kills the DOM when called: need new clean OO version of the JS tag #5775

Closed
anonymous-matomo-user opened this issue Sep 5, 2008 · 29 comments
Assignees
Labels
Bug For errors / faults / flaws / inconsistencies etc. Critical Indicates the severity of an issue is very critical and the issue has a very high priority.
Milestone

Comments

@anonymous-matomo-user
Copy link

Hi,

First, thanks for Piwik, it look greats, I’m sure it will even get better.
Second, I filled this ticket as bug, because I did not expect piwik.js→piwik_log() to wipe out my entire DOM when called once DOM has been loaded.

Could it be possible to remove any call to document.write or document.writeln from within piwik.js (or make them optional, or put them into generated javascript inserted on each page)?

The reason is the following:
1. I don’t like having javascript within html doc, I prefer to have all my js code in dedicated .js files => I moved js code into outside script
(benefit: code cleaner + most importantly does not interfer with page execution (on my configuration, piwik.js is located on another hostname => my page load freezes up until the script piwik.js is loaded if I use javascript tags, with the consequence of delaying dom load and other js code execution for several seconds).
2. when you call document.write/document.writeln once your dom has been loaded, it clears all page content => calling piwik_log() once dom is loaded clears all page content

If you could simply remove “document.writeln(’<img src=”’+_pk_src+’" alt=“Piwik” style=“border:0” />’);" from within piwik.js (or make it optional using a parameter on piwik_log function), it would enhance a lot piwik flexibility (and make my day :-)).

Below is the js code I use (based on mootools1.2) to load piwik.js and call piwik_log from external .js file once all DOM is loaded (maybe it can be useful to other people):

```
window.addEvent(‘domready’, function() {

function logPiwikData() { try { var piwik_loaded = !!piwik_log; } catch (x) { var piwik_loaded = null; } if (piwik_loaded) { piwik_log.apply(this, arguments); } else { arguments.callee.delay(500, this, arguments); // piwik.js not loaded yet } } var pkBaseURL = ((“https:” == document.location.protocol) ? “https://piwik.qc4web.com/” : “http://piwik.qc4web.com/”); var piwik_action_name = document.title; var piwik_idsite = 1; var piwik_url = pkBaseURL + “piwik.php”; new Element(‘script’).set(‘src’, pkBaseURL + ‘piwik.js’).set(‘type’,‘text/javascript’).injectInside(document.body); logPiwikData(piwik_action_name, piwik_idsite, piwik_url);

});
```

If you try it without commenting out document.writeln call from piwik.js, all your page content is wiped out. Once it’s commented, everything works smoothly.

Regards,
Remy

@mattab
Copy link
Member

mattab commented Sep 7, 2008

You are quite right that this is a bug. One solution could be to create an image in Javascript and append this image to the DOM, avoiding the document.write

Actually we would like to rewrite the whole piwik.ks javascript code to use an object oriented structure. If someone would be ready to help on this story, let me know! :)

@anonymous-matomo-user
Copy link
Author

Hi Matt,

I’ve made such implementation. It’s quite long to write it all here, I’ve set it up in my blog: http://qc4blog.com/?p=114

You can use the source code as you want. I hope you will find it usefull.
If you have any question, do not hesitate.

By the way, when generating code to be inserted within all pages, it could be useful to add “?idsite=N” to <noscript> url so that it will get to correct id site.

Regards,
Remy

@mattab
Copy link
Member

mattab commented Oct 10, 2008

Note: it obviously needs heavy testing
- on all majors browsers last versions + popular sub versions
- needs to be compared to GA and other to check that numbers are still the same

@mattab
Copy link
Member

mattab commented Oct 13, 2008

See also the change suggested in the piwik-hackers mailing list: http://lists.piwik.org/pipermail/piwik-hackers/2008-August/000337.html

that would make the tracker work in XHTML

@mattab
Copy link
Member

mattab commented Oct 14, 2008

See also:

Google analytics Tracking API seems really powerful. We wouldn’t need so much but the concept is good. http://code.google.com/apis/analytics/docs/gaJSApi.html

@mattab
Copy link
Member

mattab commented Oct 21, 2008

Also consider to build in “time tracking” to show a report of the speed of the website, enabled via a boolean in the javascript. Does the tracker need to be at top?

@mattab
Copy link
Member

mattab commented Oct 21, 2008

See also microsoft javascript API: https://help.live.com/help.aspx?mkt=en-us&project=adCenter_Analytics_rtw&querytype=keyword&query=7101yek&tmt=frmReport&domain=adcenter.microsoft.com&format=b1

http://analytics.live.com/Analytics/msAnalytics.js

@mattab
Copy link
Member

mattab commented Oct 23, 2008

see also yahoo JS: http://web.analytics.yahoo.com/indextools.js

@mattab
Copy link
Member

mattab commented Nov 18, 2008

We should also consider reusing the open source google analytics flash library to track piwik flash events: http://code.google.com/apis/analytics/docs/flashTrackingSetupFlash.html

@mattab
Copy link
Member

mattab commented Dec 26, 2008

Interesting suggestion from the forums, maybe worth adding to the requirements:
http://forum.piwik.org/index.php?showtopic=57

I am newbie in JavaScript, but is it possible create “google analytics like” javascript code?

I mean: insert only short script tag(s) with link in monitoring pages like this

<script src=“http://my_site.com/piwik.js” type=“text/javascript”></script>
<script type=“text/javascript”>some_site_identifier=XXX</script>

@robocoder
Copy link
Contributor

And perhaps some ideas from http://sourceforge.net/projects/subspace/

@robocoder
Copy link
Contributor

As reported on piwik-hacker, piwik.js doesn’t recognize <AREA> elements. (Workaround is to use <A>.)

@mattab
Copy link
Member

mattab commented Jan 22, 2009

Attachment: Remy Damour work on rewriting piwik.js in OO
[piwik_oop-20081023.tar.gz](http://issues.piwik.org/attachments/5775/piwik_oop-20081023.tar.gz)

@mattab
Copy link
Member

mattab commented Jan 22, 2009

microsoft analytics readable code can be found on http://utilitybase.com/paste/11219 (for information only)

@mattab
Copy link
Member

mattab commented Jan 22, 2009

- we have to keep backward compatibility by providing old functions as bridges to the new OO class
- whenever applicable, we should copy google analytics JS API, for simplicity of use and clarity. API doc available on http://code.google.com/apis/analytics/docs/gaJSApi.html. One suggested difference: piwik public methods don’t start with _ ; private methods start with _
- I confirm that we should drop the DOM ready condition, drop usage of global variables
- you mention #151 but doesn’t seem related?
- existing tracking options (see http://piwik.org/docs/javascript-tag-documentation/ ) should have a simple OO alternative (setDownloadExtensions, setLinkTrackingTimer, disableLinkTracking, etc.). Old way of setting global variables piwik_\* should be deprecated. the new code is backward compatible (for example, the init method reads the piwik_\* and calls the right newly named methods).
- Reponse to: 6. Change call from piwik_log(actionName, idSite, piwikURL) to Piwik.init(optionsArray).
Rather than using this technique, the final JS code should look simpler that the current one, and more like the GA script, ie.

```
[…]
var pageTracker = piwik.getTracker(piwikSiteId, piwikUrl);
pageTracker.trackPageview(); // or pageTracker.trackPageview(document.title);
[…]
```

@mattab
Copy link
Member

mattab commented Feb 9, 2009

- as a new requirement, we shall make sure that Piwik doesn’t produce javascript error when the piwik.js is blocked by ad-blocking extensions. Currently the code tries to call piwik_log which is not defined and results in a javascript error. [See forum for more info](whos.amung.us).

@robocoder
Copy link
Contributor

See also ticket #549.

@mattab
Copy link
Member

mattab commented Feb 17, 2009

- new requirement: piwik.js would provide a`
setDocumentTitle()`. This method would set the “title” http parameter to piwik.php to the custom value. This value would then be used to build best pages by title, see #530
- Piwik currently build the “actions” report based on URL, or based on the custom`
piwik_action_name = ‘’;` value if specified. If not, the report is built based on the URL. This existing`
piwik_action_name = ’’;` would logically be replaced by a`
piwik.setUrl()`. The parameter to piwik.php would be renamed from`
action_name` to`
custom_url` or similar.

This is open to suggestions..

@mattab
Copy link
Member

mattab commented Feb 17, 2009

for interest, [Yahoo Web Analytics Javascript API](http://help.yahoo.com/l/us/yahoo/smallbusiness/store/analytics/advanced/advanced-02.html)

@anonymous-matomo-user
Copy link
Author

has anyone figured out a way around this yet. I am attempting to implement tracking from within a swf file and the only thing keeping this from completion is that the DOM blanks out after the piwik_log is called

@anonymous-matomo-user
Copy link
Author

@doodlez, you might find this useful: http://www.qc4blog.com/?p=114
oop js file (not using document.write):
http://qc4blog.com/wp-content/uploads/2008/09/piwik_oop-029.js

Regards, Remy

@anonymous-matomo-user
Copy link
Author

Replying to doodlez:

has anyone figured out a way around this yet. I am attempting to implement tracking from within a swf file and the only thing keeping this from completion is that the DOM blanks out after the piwik_log is called

@doodlez, you might find this useful: http://www.qc4blog.com/?p=114 oop js file (not using document.write):

http://qc4blog.com/wp-content/uploads/2008/09/piwik_oop-029.js

Regards, Remy
(oups, missed reply button)

@anonymous-matomo-user
Copy link
Author

@doodlez, you might find this useful: http://www.qc4blog.com/?p=114 oop js file (not using document.write):

http://qc4blog.com/wp-content/uploads/2008/09/piwik_oop-029.js

Regards, Remy
(oups, missed reply button)

I attempted to integrate your code, but it didn't appear to be tracking.

@anonymous-matomo-user
Copy link
Author

this is what I attempted to implement:

<script language="javascript" src="http://www.****.com/piwik/piwik_oop-0.2.9-min.js" type="text/javascript"></script>
<script type="text/javascript">
function setActionName(page){
piwik_action_name = page;
piwik_idsite = 1;
piwik_url = "http://www.****.com/piwik/piwik.php";
piwik_log(piwik_action_name, piwik_idsite, piwik_url);
}
</script><object>

<noscript><p>Web analytics <img src="http://www.****.com/piwik/piwik.php?idsite=1" style="border:0" alt="piwik"/></p>
</noscript></object>

@mattab
Copy link
Member

mattab commented May 18, 2009

this will be the main new feature in Piwik 0.4 release.

@robocoder
Copy link
Contributor

(In [1151]) fixes #5775 - OO version of piwik.js which no longer modifies DOM;
remove misc/testJavascripTracker (interactive tests);
add tests/javascript (QUnit unit tests);
fixes #661 - use click event instead of mousedown;
fixes #549 - define your own download/outlink tracking classes;
fixes #5502 - add hook interface for module

@robocoder
Copy link
Contributor

(In [1157]) refs #5775 - minor changes per review

@mattab
Copy link
Member

mattab commented Jun 2, 2009

(In [1177]) - refs #5775 now displaying the new OO JS tag by default in install & in admin UI + showing some help + renaming the "js code" to consistent "JavaScript Tracking Tag"

@robocoder
Copy link
Contributor

(In [1185]) refs #5775 - accommodate malformed URIs that contain an illegal space
after the pseudo-protocol

@anonymous-matomo-user anonymous-matomo-user added this to the Piwik 0.4 milestone Jul 8, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For errors / faults / flaws / inconsistencies etc. Critical Indicates the severity of an issue is very critical and the issue has a very high priority.
Projects
None yet
Development

No branches or pull requests

3 participants