Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
insert_ga_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 
21 
22 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_INSERT_GA_FILTER_H_
23 #define NET_INSTAWEB_REWRITER_PUBLIC_INSERT_GA_FILTER_H_
24 
33 
34 namespace net_instaweb {
35 
36 class Statistics;
37 class Variable;
38 
40 extern const char kGAExperimentSnippet[];
41 extern const char kGAJsSnippet[];
42 extern const char kAnalyticsJsSnippet[];
43 extern const char kAnalyticsJsIncreaseSiteSpeedTracking[];
44 extern const char kAnalyticsJsIncreaseSiteSpeedTrackingMinimal[];
45 extern const char kContentExperimentsJsClientUrl[];
46 extern const char kContentExperimentsNonNumericVariantComment[];
47 extern const char kContentExperimentsSetChosenVariationSnippet[];
48 extern const char kContentExperimentsSetExpAndVariantSnippet[];
49 extern const char kGASpeedTracking[];
50 
54 class InsertGAFilter : public CommonFilter {
55  public:
56  explicit InsertGAFilter(RewriteDriver* rewrite_driver);
57  virtual ~InsertGAFilter();
58 
60  static void InitStats(Statistics* stats);
61 
62  virtual void StartDocumentImpl();
63  virtual void StartElementImpl(HtmlElement* element);
64  virtual void EndElementImpl(HtmlElement* element);
66  virtual void Characters(HtmlCharactersNode* characters);
67 
68  virtual const char* Name() const { return "InsertGASnippet"; }
69  ScriptUsage GetScriptUsage() const override { return kWillInjectScripts; }
70 
71  private:
72  enum AnalyticsStatus {
73  kGaJs,
74  kAnalyticsJs,
75  kNoSnippetFound,
76  kUnusableSnippetFound,
77  };
79 
81  enum ParseState {
82  kInitial,
83  kGotGa,
84  kGotGaFuncCall,
85  kGotGaCreate,
86  kGotGaSend,
87  kGotGaCreateComma,
88  kGotFieldsObject,
89  kGotGaSendComma,
90  kGotGaSendPageview,
91  kSuccess
92  };
93 
95  GoogleString ConstructExperimentSnippet() const;
96 
98  virtual void EndDocument();
99 
101  void HandleEndScript(HtmlElement* script);
102 
108  void RewriteInlineScript(HtmlCharactersNode* characters);
109 
116  AnalyticsStatus FindSnippetInScript(const GoogleString& s);
117 
119  GoogleString AnalyticsJsExperimentSnippet() const;
120  GoogleString GaJsExperimentSnippet() const;
121 
124  bool ShouldInsertExperimentTracking(bool analytics_js);
125 
126  bool StringLiteralMatches(StringPiece literal, StringPiece desired);
127  bool StringLiteralEndsWith(StringPiece literal, StringPiece desired);
128 
130  Variable* inserted_ga_snippets_count_;
131 
134  HtmlElement* script_element_;
136  bool added_analytics_js_;
138  bool added_experiment_snippet_;
139 
141  GoogleString ga_id_;
142 
145  bool found_snippet_;
146 
148  bool increase_speed_tracking_;
149 
153  bool seen_sync_ga_js_;
154 
158  GoogleString postponed_script_body_;
159 
160 
161 };
162 
163 }
164 
165 #endif
Definition: common_filter.h:47
static void InitStats(Statistics *stats)
Set up statistics for this filter.
Leaf node representing raw characters in HTML.
Definition: html_node.h:167
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Definition: html_element.h:42
virtual void StartDocumentImpl()
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
ScriptUsage GetScriptUsage() const override
Definition: insert_ga_filter.h:69
Definition: insert_ga_filter.h:54
const char kGAExperimentSnippet[]
Visible only for use in tests.
ScriptUsage
Describes a filter's relationship with scripts.
Definition: html_filter.h:38
Definition: rewrite_driver.h:100
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: insert_ga_filter.h:68
virtual void Characters(HtmlCharactersNode *characters)
HTML Events we expect to be in <script> elements.