Add piwik tracking code to IIS

Source: Internet
Author: User
Tags piwik

I haven't written a blog for a long time...
Today, I want to write about how to add piwik tracking to IIS.Code.
Background:
• Piwik open-source PHP Site StatisticsProgramHttp://piwik.org
• Httpmodule: http://www.cnblogs.com/stwyhm/archive/2006/08/09/471729.html
Implementation idea: Use httpmodule to add the statistical code before the body of the ASPX page.
Code implementation:

1. Create a DLL Project

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. io; namespace piwikanalytics {// modified to the popular Google Analytics (https://www.google.com/analytics/) script public class piwikanalyticsmodule: ihttpmodule {static St Ring googlescript = string. empty; httpapplication application; Public void dispose () {// context. beginrequest-= new eventhandler (onbeginrequest);} public void Init (httpapplication context) {context. beginrequest + = new eventhandler (onbeginrequest); Application = context;} void onbeginrequest (Object sender, eventargs e) {httpapplication application = (httpapplication) sender; // web. website words defined in config String. The string sites = configurationmanager. etettings ["piwikanalyticssites"] will be introduced later. // No exception is handled here. Remember to add it when releasing it ~ String [] sitearr = sites. split (';'); string siteid = "5"; foreach (string site in sitearr) {If (application. request. URL. originalstring. tolower (). indexof (site. split (',') [0])> 0) {siteid = site. split (',') [1] ;}} googlescript = @ "<! -- Piwik --> <SCRIPT type = "" text/JavaScript ""> var pkbaseurl = ("" https: "" = Document. Location. Protocol )? "" Https://yourpiwik.com/piwik/ "": "" http://yourpiwik.com/piwik/ ""); document. write (Unescape ("" % 3 cscript src = '"" + pkbaseurl + "" piwik. JS 'Type = 'text/JavaScript '% 3E % 3C/script % 3e "")); </SCRIPT> <SCRIPT type = "" text/JavaScript ""> // try {var piwiktracker = piwik. gettracker (pkbaseurl + "" piwik. PHP "", "+ siteid + @"); piwiktracker. trackpageview (); piwiktracker. enablelinktracking (); //} catch (ERR ){} </SCRIPT> <NoScript> <p>  </P> </NoScript> <! -- End piwik tag --> "; // The string [] temp = application must be blocked for the ASPX page. request. currentexecutionfilepath. split ('. '); If (temp. length> 0 & temp [temp. length-1]. tolower () = "aspx") {application. response. filter = new analyticsstream (application. response. filter) ;}} class analyticsstream: stream {stream innerstream; memorystream memory = new memorystream (); Public analyticsstream (Stream innerstre Am) {This. innerstream = innerstream;} public override void close () {memory. position = 0; using (streamwriter writer = new streamwriter (innerstream) {using (streamreader reader = new streamreader (memory) {While (! Reader. endofstream) {// find </body> If (matchesorwrite (reader, writer, '<', null) & matchesorwrite (reader, writer, '/', "<") & matchesorwrite (reader, writer, 'B', "</") & matchesorwrite (reader, writer, 'O', "</B ") & matchesorwrite (reader, writer, 'D', "</bo") & matchesorwrite (reader, writer, 'y', "</bod ") & matchesorwrite (reader, writer, '>', "</body") {// string script = string. format (goo Glescript, accountnumber) + "</body>"; writer. Write (googlescript); While (! Reader. endofstream) writer. write (char) reader. read () ;}}} base. close ();} private bool matchesorwrite (streamreader reader, streamwriter writer, char target, string buffered) {If (! Reader. endofstream) {char current = (char) reader. read (); If (current = target) {return true;} else {writer. write (buffered); writer. write (current) ;}} else {writer. write (buffered);} return false;} public override bool Canread {get {return memory. canread;} public override bool canseek {get {return memory. canseek ;}} public override bool canwrite {get {return memory. canwrite ;}} public override void flush () {memory. flush ();} public override long length {get {return memory. length ;}} public override long position {get {return memory. position;} set {memory. position = value ;}} public override int read (byte [] buffer, int offset, int count) {return memory. read (buffer, offset, count);} public override long seek (long offset, seekorigin origin) {Throw new notimplementedexception ();} public override void setlength (long value) {memory. setlength (value);} public override void write (byte [] buffer, int offset, int count) {memory. write (buffer, offset, count );}}}}

2.
compile piwikanalytics. dll and register it in GAC ~], Otherwise, copy each sub-site.
How to forcibly name an assembly in Visual Studio: (recommended) http://www.cnblogs.com/awpatp/archive/2010/02/07/1665530.html
the article also mentions that if you get the publickeytoken of an assembly, in fact, this is not so troublesome. Put the Assembly in GAC, and a column will show the ~ of publickeytoken ~.
• GAC registration: http://www.cnblogs.com/mljmalongjiang/archive/2008/07/31/1257135.html
• There is also a simpler way to directly place the DLL to C: \ windows \ Assembly folder [recommended]
another way of strong naming Assembly: http://www.cnblogs.com/zhongge/articles/1207183.html
after the above steps, we have put piwikanalytics. DLL registered to GAC, then we are on the web. the configuration in config is complete!
3. default path c: \ Inetpub \ wwwroot for Web. config ~

 
   
   
    
      
     
      
      
       
      
      
       
        
       
      
     
   
   

remember to replace the publickeytoken obtained in step 2 with your own one ~
restart IIS to refresh the page. Check it in piwik again. That's all!
Note:
1. when registering a module, configure it according to your actual situation
2. the above method is not applicable to MVC and template mode development
3. every time you rewrite the code, you need to re-generate the DLL, re-register the GAC, and recycle the corresponding application pool of the site.
more: two pwiki client viewing software
• Desktop Web analytics
• piwik connector
over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.