Page Speed Optimization Libraries  1.13.35.1
 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 
59 
60 namespace net_instaweb {
61 class HtmlCdataNode;
62 class HtmlCharactersNode;
63 class HtmlCommentNode;
64 class HtmlElement;
65 class HtmlIEDirectiveNode;
66 class HtmlParse;
67 class Statistics;
68 class Variable;
69 
70 
72 class ScriptEditor {
73  public:
74  enum Type {
75  kGaJsScriptSrcLoad = 0,
76  kGaJsDocWriteLoad,
77  kGaJsInit,
78  };
79  ScriptEditor(HtmlElement* script_element_,
80  HtmlCharactersNode* characters_node,
81  GoogleString::size_type pos,
82  GoogleString::size_type len,
83  Type editor_type);
84 
85  HtmlElement* GetScriptElement() const { return script_element_; }
86  HtmlCharactersNode* GetScriptCharactersNode() const {
87  return script_characters_node_;
88  }
89  Type GetType() const { return editor_type_; }
90 
91  void NewContents(const StringPiece &replacement,
92  GoogleString* contents) const;
93 
94  private:
95  HtmlElement* script_element_;
96  HtmlCharactersNode* script_characters_node_;
97 
98  GoogleString::size_type pos_;
99  GoogleString::size_type len_;
100 
101  Type editor_type_;
102 
103 };
104 
105 
109  public:
110  typedef StringPieceVector MethodVector;
111 
112  explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
113  Statistics* statistics);
114  virtual ~GoogleAnalyticsFilter();
115 
117  explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
118  Statistics* statistics,
119  MethodVector* glue_methods,
120  MethodVector* unhandled_methods);
121 
122  static void InitStats(Statistics* statistics);
123 
124  virtual void StartDocument();
125  virtual void EndDocument();
126  virtual void StartElement(HtmlElement* element);
127  virtual void EndElement(HtmlElement* element);
128 
129  virtual void Flush();
130 
132  virtual void Characters(HtmlCharactersNode* characters_node);
133 
135  virtual void Comment(HtmlCommentNode* comment);
136  virtual void Cdata(HtmlCdataNode* cdata);
137  virtual void IEDirective(HtmlIEDirectiveNode* directive);
138 
139  virtual const char* Name() const { return "GoogleAnalytics"; }
140  ScriptUsage GetScriptUsage() const override { return kWillInjectScripts; }
141 
142  static const char kPageLoadCount[];
143  static const char kRewrittenCount[];
144 
145  private:
146  void ResetFilter();
147 
148  bool MatchSyncLoad(StringPiece contents,
149  GoogleString::size_type* pos,
150  GoogleString::size_type* len) const;
151  bool MatchSyncInit(StringPiece contents,
152  GoogleString::size_type start_pos,
153  GoogleString::size_type* pos,
154  GoogleString::size_type* len) const;
155  bool MatchUnhandledCalls(StringPiece contents,
156  GoogleString::size_type start_pos) const;
157  void FindRewritableScripts();
158  void GetSyncToAsyncScript(GoogleString* buffer) const;
159  bool RewriteAsAsync();
160 
161  bool is_load_found_;
162  bool is_init_found_;
163  std::vector<ScriptEditor*> script_editors_;
164 
165  scoped_ptr<MethodVector> glue_methods_;
166  scoped_ptr<MethodVector> unhandled_methods_;
167 
168  HtmlParse* html_parse_;
169  HtmlElement* script_element_;
170  HtmlCharactersNode* script_characters_node_;
171 
172  Variable* page_load_count_;
173  Variable* rewritten_count_;
174 
175 
176 };
177 
178 }
179 
180 #endif
virtual void StartElement(HtmlElement *element)
Definition: google_analytics_filter.h:108
virtual void Cdata(HtmlCdataNode *cdata)
Called for CDATA blocks (e.g. <![CDATA[foobar]]>)
Definition: statistics.h:43
Leaf node representing raw characters in HTML.
Definition: html_node.h:167
Definition: empty_html_filter.h:37
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Definition: html_parse.h:88
virtual void IEDirective(HtmlIEDirectiveNode *directive)
Definition: html_element.h:42
ScriptUsage GetScriptUsage() const override
Definition: google_analytics_filter.h:140
Leaf node representing an HTML comment.
Definition: html_node.h:193
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
ScriptUsage
Describes a filter's relationship with scripts.
Definition: html_filter.h:38
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: google_analytics_filter.h:139
virtual void Characters(HtmlCharactersNode *characters_node)
Expected HTML Events in <script> elements.
Leaf node representing a CDATA section.
Definition: html_node.h:147
Leaf node representing an HTML IE directive.
Definition: html_node.h:213
Edit a substring in a script element.
Definition: google_analytics_filter.h:72
virtual void Comment(HtmlCommentNode *comment)
Unexpected HTML Events in <script> elements.