forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsFilter.cs
More file actions
129 lines (108 loc) · 3.77 KB
/
AnalyticsFilter.cs
File metadata and controls
129 lines (108 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Configuration;
namespace Ext.Net.MVC.Examples
{
public class AnalyticsFilter : Stream
{
private readonly Stream response;
private readonly StringBuilder html;
public const string ANALYTIC_SCRIPT = @"
<script type=""text/javascript"">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19135912-3']);
_gaq.push(['_setDomainName', '.ext.net']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setAllowHash', false]);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
var GoSquared = {};
GoSquared.acct = 'GSN-436148-U';
(function(w){
function gs(){
w._gstc_lt = +new Date;
var d = document, g = d.createElement('script');
g.type = 'text/javascript';
g.src = '//d1l6p2sc9645hc.cloudfront.net/tracker.js';
var s = d.getElementsByTagName('script')[0];
s.parentNode.insertBefore(g, s);
}
w.addEventListener ? w.addEventListener('load', gs, false) : w.attachEvent('onload', gs);
})(window);
</script>";
public AnalyticsFilter(Stream stream)
{
this.response = stream;
this.html = new StringBuilder();
}
public override void Flush()
{
if (this.html.Length == 0)
{
this.response.Flush();
return;
}
string html = this.html.ToString();
if (Convert.ToBoolean(WebConfigurationManager.AppSettings["GoogleAnalytics"]))
{
int index = html.LastIndexOf("</body>", StringComparison.InvariantCultureIgnoreCase);
if (index >= 0)
{
this.html.Insert(index, ANALYTIC_SCRIPT);
}
}
byte[] data = System.Text.Encoding.UTF8.GetBytes(this.html.ToString());
this.response.Write(data, 0, data.Length);
this.response.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
this.html.Append(HttpContext.Current.Response.ContentEncoding.GetString(buffer, offset, count));
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Close()
{
this.response.Close();
}
public override long Length
{
get { return 0; }
}
private long position;
public override long Position
{
get { return this.position; }
set { this.position = value; }
}
public override long Seek(long offset, SeekOrigin origin)
{
return this.response.Seek(offset, origin);
}
public override void SetLength(long length)
{
this.response.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return this.response.Read(buffer, offset, count);
}
}
}