Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
google_analytics_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
47 
48 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_GOOGLE_ANALYTICS_FILTER_H_
49 #define NET_INSTAWEB_REWRITER_PUBLIC_GOOGLE_ANALYTICS_FILTER_H_
50 
51 #include <vector>
52 
53 #include "pagespeed/kernel/base/basictypes.h"
54 #include "pagespeed/kernel/base/scoped_ptr.h"
55 #include "pagespeed/kernel/base/string.h"
56 #include "pagespeed/kernel/base/string_util.h"
57 #include "pagespeed/kernel/html/empty_html_filter.h"
58 
59 namespace net_instaweb {
60 class HtmlCdataNode;
61 class HtmlCharactersNode;
62 class HtmlCommentNode;
63 class HtmlElement;
64 class HtmlIEDirectiveNode;
65 class HtmlParse;
66 class Statistics;
67 class Variable;
68 
69 
71 class ScriptEditor {
72  public:
73  enum Type {
74  kGaJsScriptSrcLoad = 0,
75  kGaJsDocWriteLoad,
76  kGaJsInit,
77  };
78  ScriptEditor(HtmlElement* script_element_,
79  HtmlCharactersNode* characters_node,
80  GoogleString::size_type pos,
81  GoogleString::size_type len,
82  Type editor_type);
83 
84  HtmlElement* GetScriptElement() const { return script_element_; }
85  HtmlCharactersNode* GetScriptCharactersNode() const {
86  return script_characters_node_;
87  }
88  Type GetType() const { return editor_type_; }
89 
90  void NewContents(const StringPiece &replacement,
91  GoogleString* contents) const;
92 
93  private:
94  HtmlElement* script_element_;
95  HtmlCharactersNode* script_characters_node_;
96 
97  GoogleString::size_type pos_;
98  GoogleString::size_type len_;
99 
100  Type editor_type_;
101  DISALLOW_COPY_AND_ASSIGN(ScriptEditor);
102 };
103 
104 
107 class GoogleAnalyticsFilter : public EmptyHtmlFilter {
108  public:
109  typedef StringPieceVector MethodVector;
110 
111  explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
112  Statistics* statistics);
113  virtual ~GoogleAnalyticsFilter();
114 
116  explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
117  Statistics* statistics,
118  MethodVector* glue_methods,
119  MethodVector* unhandled_methods);
120 
121  static void InitStats(Statistics* statistics);
122 
123  virtual void StartDocument();
124  virtual void EndDocument();
125  virtual void StartElement(HtmlElement* element);
126  virtual void EndElement(HtmlElement* element);
127 
128  virtual void Flush();
129 
131  virtual void Characters(HtmlCharactersNode* characters_node);
132 
134  virtual void Comment(HtmlCommentNode* comment);
135  virtual void Cdata(HtmlCdataNode* cdata);
136  virtual void IEDirective(HtmlIEDirectiveNode* directive);
137 
138  virtual const char* Name() const { return "GoogleAnalytics"; }
139 
140  static const char kPageLoadCount[];
141  static const char kRewrittenCount[];
142 
143  private:
144  void ResetFilter();
145 
146  bool MatchSyncLoad(StringPiece contents,
147  GoogleString::size_type* pos,
148  GoogleString::size_type* len) const;
149  bool MatchSyncInit(StringPiece contents,
150  GoogleString::size_type start_pos,
151  GoogleString::size_type* pos,
152  GoogleString::size_type* len) const;
153  bool MatchUnhandledCalls(StringPiece contents,
154  GoogleString::size_type start_pos) const;
155  void FindRewritableScripts();
156  void GetSyncToAsyncScript(GoogleString* buffer) const;
157  bool RewriteAsAsync();
158 
159  bool is_load_found_;
160  bool is_init_found_;
161  std::vector<ScriptEditor*> script_editors_;
162 
163  scoped_ptr<MethodVector> glue_methods_;
164  scoped_ptr<MethodVector> unhandled_methods_;
165 
166  HtmlParse* html_parse_;
167  HtmlElement* script_element_;
168  HtmlCharactersNode* script_characters_node_;
169 
170  Variable* page_load_count_;
171  Variable* rewritten_count_;
172 
173  DISALLOW_COPY_AND_ASSIGN(GoogleAnalyticsFilter);
174 };
175 
176 }
177 
178 #endif
Definition: google_analytics_filter.h:107
virtual void Characters(HtmlCharactersNode *characters_node)
Expected HTML Events in <script> elements.
Edit a substring in a script element.
Definition: google_analytics_filter.h:71
virtual void Comment(HtmlCommentNode *comment)
Unexpected HTML Events in <script> elements.