@@ -15,23 +15,14 @@ Measurement library will provide an open source alternative for sites/apps to se
15
15
- [ Usage] ( #usage )
16
16
- [ Event Command] ( #event-command )
17
17
- [ Set Command] ( #set-command )
18
- - [ Event Processors] ( #event-processors )
19
- - [ Google Analytics] ( #google-analytics )
20
- - [ Custom Event Processor] ( #custom-event-processor )
21
- - [ Storage Interfaces] ( #storage-interfaces )
22
- - [ Cookies] ( #cookies )
23
- - [ Custom Storage Interface] ( #custom-storage-interface )
24
- - [ Local Setup] ( #local-setup )
25
- - [ Creating the build] ( #creating-the-build )
26
- - [ Running Tests Interactively] ( #running-tests-interactively )
27
18
28
19
# Overview
29
20
Measurement Library is a utility to help developers send data collected from their website to
30
21
other libraries for analytics. After specifying settings in a script tag, you can process any
31
22
sent events using a variety of built in event processors (e.g. sending data to Google Analytics).
32
23
33
24
Data from events or generated by event processors is automatically saved to persist between visits using your choice of
34
- a variety of different storage options (e.g. cookies).
25
+ a variety of different storage options (e.g. cookies). [ Read the docs ] ( https://googleinterns.github.io/measurement-library/# ) to learn more.
35
26
36
27
# Installation
37
28
First, install the package with your favorite package manager:
@@ -41,152 +32,46 @@ yarn add measurement-library
41
32
npm install measurement-library
42
33
```
43
34
44
- Next, choose [ the event processor] ( #event-processors ) and [ the storage implementation] ( #storage-interfaces )
35
+ Next, choose [ the event processor] ( https://googleinterns.github.io/measurement-library/#EventProcessors ) and
36
+ [ the storage implementation] ( (https://googleinterns.github.io/measurement-library/#StorageInterfaces) )
45
37
to use and include this code in your ` <head> ` tag. It should be placed as high up in the block as possible
46
38
in order to catch users who leave immediately after loading the page, then immediately leave.
47
39
``` html
48
40
<!-- Measurement Library Snippet -->
49
- <script async src =" . /node_modules/measurement-library/dist/measure" />
41
+ <script async src =" /node_modules/measurement-library/dist/measure" />
50
42
< script>
51
43
window .dataLayer = window .dataLayer || [];
52
44
function measure (){dataLayer .push (arguments );}
53
- // One or more config commands. To send data to google analytics while
45
+ // One or more config commands. To send data to google analytics while
54
46
// using cookies for storage:
55
- measure (' config' , ' googleAnalytics' , {}, ' cookies' , {});
47
+ measure (' config' , ' googleAnalytics' , {
48
+ ' measurement_id' : YOUR_MEASUREMENT_ID ,
49
+ ' api_secret' : YOUR_API_SECRET ,
50
+ }, ' cookies' , {});
56
51
</script >
57
52
```
58
53
59
- [ // ] : # ( TODO kj: Make the config command use the right settings for google analytics )
60
-
61
54
As an alternative, you can use the development version when testing your page. The dev version has a bigger file size,
62
55
but reports possible errors to the console.
63
56
``` html
64
57
<!-- Measurement Library Snippet (Development Version) -->
65
- <script async src =" . /node_modules/measurement-library/dist/measure-debug" />
58
+ <script async src =" /node_modules/measurement-library/dist/measure-debug" />
66
59
< script> /* Configure script like last example. */ </script >
67
60
```
68
61
69
62
# Usage
70
- After setting up the script tags, you can begin to use the library. All commands
71
- are processed by passing arguments to the ` measure() ` function.
63
+ After setting up the script tag using the ` config ` command, you can begin to use the library. All commands
64
+ are processed by passing arguments to the ` measure() ` function.
65
+
72
66
73
67
## Event command
74
68
When you run the command ` measure('event', eventName, eventOptions) ` , all registered
75
- [ event processors] ( #event-processors ) will read the event and perform actions corresponding
69
+ [ event processors] ( https://googleinterns.github.io/measurement-library/#EventProcessors ) will read the event and perform actions corresponding
76
70
to their API.
77
71
78
72
## Set command
79
73
To save parameters beyond the current page, you can call the command ` measure('set', key, value) ` .
80
74
The registered event processor will determine if the value should be saved, how long the value should
81
- be saved for, and save it. To overwrite this behavior, you can specify a third time-to-live parameter:
82
- ` measure('set', key, value, secondsToLive) ` . In particular, if ` secondsToLive ` is 0, no data will be saved
83
- to long term storage.
84
-
85
- The set command can also be used to set parameters related to a implementation of
86
- an event processor or storage interface. For example, to modify the default parameters
87
- of the cookies storage, run a ` set ` command in the script tag before calling
88
- ` config ` .
89
-
90
- ``` js
91
- // Set the default cookie parameters
92
- measure (' set' , ' cookies' , {prefix: ' my_' , expires: 11 });
93
- // Use the default cookie parameters: prefix 'my_' and expires 11.
94
- measure (' config' , ' eventProcessorName' , {}, ' cookies' , {});
95
- // Override a default cookie parameter: expires is 22 for this storage.
96
- measure (' config' , ' eventProcessorName' , {}, ' cookies' , {expires: 22 });
97
- ```
98
-
99
- # Event Processors
100
- ## Google Analytics
101
- [ // ] : # ( TODO: kj )
102
-
103
- ## Custom Event Processor
104
- If none of the built-in processors fit your needs, you can create your own event processor.
105
- An event processor is a class with the 2 methods described in [ eventProcessorInterface.js] ( /src/eventProcessor/EventProcessorInterface.js )
106
- in addition to a constructor that takes an options object as it's only parameter.
107
-
108
- ``` html
109
- <script async src =" ./node_modules/measurement-library/dist/measure" />
110
- < script>
111
- window .dataLayer = window .dataLayer || [];
112
- function measure (){dataLayer .push (arguments );}
113
- class MyProcessor {
114
- constructor ({theNumber});
115
- processEvent(storageInterface, modelInterface, eventName, eventOptions);
116
- persistTime(key, value);
117
- }
118
- // Call the class constructor
119
- measure (config, myProcessor, {theNumber: 42 }, ' cookies' , {});
120
- // Calls the processEvent function with parameters
121
- // (cookies storage, short term storage, 'jump', {height: 6}).
122
- measure (' event' , ' jump' , {height: 6 })
123
- // Sets a value in the storage with secondsToLive determined by
124
- // the persistTime function of the event processor.
125
- measure (' set' , ' key' , ' value' );
126
- < / script>
127
- ` ` `
128
-
129
- # Storage Interfaces
130
- ## Cookies
131
- [//]: # (TODO: pedro)
132
-
133
- ## Custom Storage Interface
134
- If none of the built-in storage systems fit your needs, you can create your own storage interface.
135
- A storage interface is a class with the 2 methods described in [storageInterface.js](/src/storage/StorageInterface.js)
136
- in addition to a constructor that takes an options object as it's only parameter.
137
-
138
- ` ` ` html
139
- < script async src= " ./node_modules/measurement-library/dist/measure" / >
140
- < script>
141
- window .dataLayer = window .dataLayer || [];
142
- function measure (){dataLayer .push (arguments );}
143
- class MyStorage {
144
- constructor ({theNumber});
145
- save(key, value, secondsToLive= 3600*24) {};
146
- load (key , defaultValue = undefined ) {};
147
- }
148
- // Call the class constructor.
149
- measure (config, ' eventProcessorName' , {}, MyStorage, {theNumber: 42 });
150
- // Call the save command with secondsToLive determined by the persistTime
151
- // function of the event processor.
152
- measure (' set' , ' key' , ' value' );
153
- < / script>
154
- ` ` `
155
-
156
- # Local Setup
157
- ## Creating The Build
158
- You will need to have either [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
159
- or [yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) installed.
160
-
161
- First, install dependencies with yarn or npm
162
- ` ` ` shell script
163
- yarn install
164
- # or
165
- npm install
166
- ` ` `
167
-
168
- Next, run the tests using yarn or npm:
169
-
170
- ` ` ` shell script
171
- yarn test
172
- # or
173
- npm run test
174
- ` ` `
175
-
176
- ## Running Tests Interactively
177
- You can also run the tests "interactively" via Karma directly.
178
-
179
- ` ` ` shell script
180
- yarn unit
181
- # or
182
- npm run unit
183
- ` ` `
184
-
185
- To run the integration tests instead of the unit tests,
186
-
187
- ` ` ` shell script
188
- yarn integration
189
- # or
190
- npm run integration
191
- ` ` `
192
- ****
75
+ be saved for, and save it in the corresponding [ storage] ( https://googleinterns.github.io/measurement-library/#StorageInterfaces ) .
76
+ To overwrite this behavior, you can specify a third time-to-live parameter:
77
+ ` measure('set', key, value, secondsToLive) ` . In particular, if ` secondsToLive ` is 0, no data will be saved to long term storage.
0 commit comments