Skip to content

Commit 9638578

Browse files
committed
Merge branch 'master' of https://github.com/googleinterns/measurement-library into pageviewSpecificDocs
2 parents 14c733c + 337b6cd commit 9638578

File tree

9 files changed

+294
-116
lines changed

9 files changed

+294
-116
lines changed

README.md

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ Measurement library will provide an open source alternative for sites/apps to se
1515
- [Usage](#usage)
1616
- [Event Command](#event-command)
1717
- [Set Command](#set-command)
18-
- [Local Setup](#local-setup)
19-
- [Creating the build](#creating-the-build)
20-
- [Running Tests Interactively](#running-tests-interactively)
2118

2219
# Overview
2320
Measurement Library is a utility to help developers send data collected from their website to
@@ -63,8 +60,9 @@ but reports possible errors to the console.
6360
```
6461

6562
# Usage
66-
After setting up the script tags, you can begin to use the library. All commands
67-
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+
6866

6967
## Event command
7068
When you run the command `measure('event', eventName, eventOptions)`, all registered
@@ -74,57 +72,6 @@ to their API.
7472
## Set command
7573
To save parameters beyond the current page, you can call the command `measure('set', key, value)`.
7674
The registered event processor will determine if the value should be saved, how long the value should
77-
be saved for, and save it. To overwrite this behavior, you can specify a third time-to-live parameter:
78-
`measure('set', key, value, secondsToLive)`. In particular, if `secondsToLive` is 0, no data will be saved
79-
to long term storage.
80-
81-
The set command can also be used to set parameters related to a implementation of
82-
an event processor or storage interface. For example, to modify the default parameters
83-
of the cookies storage, run a `set` command in the script tag before calling
84-
`config`.
85-
86-
```js
87-
// Set the default cookie parameters
88-
measure('set', 'cookies', {prefix: 'my_', expires: 11});
89-
// Use the default cookie parameters: prefix 'my_' and expires 11.
90-
measure('config', 'eventProcessorName', {}, 'cookies', {});
91-
// Override a default cookie parameter: expires is 22 for this storage.
92-
measure('config', 'eventProcessorName', {}, 'cookies', {expires: 22});
93-
```
94-
# Local Setup
95-
## Creating The Build
96-
You will need to have either [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
97-
or [yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) installed.
98-
99-
First, install dependencies with yarn or npm
100-
```shell script
101-
yarn install
102-
# or
103-
npm install
104-
```
105-
106-
Next, run the tests using yarn or npm:
107-
108-
```shell script
109-
yarn test
110-
# or
111-
npm run test
112-
```
113-
114-
## Running Tests Interactively
115-
You can also run the tests "interactively" via Karma directly.
116-
117-
```shell script
118-
yarn unit
119-
# or
120-
npm run unit
121-
```
122-
123-
To run the integration tests instead of the unit tests,
124-
125-
```shell script
126-
yarn integration
127-
# or
128-
npm run integration
129-
```
130-
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.

docs/LocalSetup.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Local Setup
2+
## Creating The Build
3+
You will need to have either [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
4+
or [yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) installed.
5+
6+
First, install dependencies with yarn or npm
7+
```shell script
8+
yarn install
9+
# or
10+
npm install
11+
```
12+
13+
Next, run the tests using yarn or npm:
14+
15+
```shell script
16+
yarn test
17+
# or
18+
npm run test
19+
```
20+
21+
## Running Tests Interactively
22+
You can also run the tests "interactively" via Karma directly.
23+
24+
```shell script
25+
yarn unit
26+
# or
27+
npm run unit
28+
```
29+
30+
To run the integration tests instead of the unit tests,
31+
32+
```shell script
33+
yarn integration
34+
# or
35+
npm run integration
36+
```
37+

docs/PersistentParameters.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Persistent Parameters
2+
## Sharing Constructor Arguments
3+
If you have many storage interfaces on the page that should have the same settings,
4+
you can call the `set` command before calling the `config` command to share parameters automatically,
5+
passing in the name of the storage interface as the first argument to `set`.
6+
7+
```js
8+
// Set the default cookie parameters
9+
measure('set', 'cookies', {prefix: 'my_', expires: 11});
10+
// Use the default cookie parameters: prefix 'my_' and expires 11.
11+
measure('config', 'googleAnalytics', {}, 'cookies', {});
12+
// Override a default cookie parameter: expires is 22 for this storage.
13+
measure('config', 'googleAnalytics', {}, 'cookies', {expires: 22});
14+
```
15+
16+
Since custom storage interfaces do not have a name, to enable the behavior of shared arguments for a custom object,
17+
you must implement the static getName() method, returning the string that should be passed as the first argument
18+
to set.
19+
20+
The example above used a storage interface, but it works exactly the same for an event processor. However,
21+
with event processors, you should think of the set command and constructor arguments as a way to share
22+
event options between processors, as detailed in the next section.
23+
24+
## Sharing Event Options
25+
If you have many processors on a page that should share several event options, then calling
26+
`measure('set','processorType', persistingOptions)` will set parameters that persist from each
27+
call to `measure('event', eventName, eventOptions)`.
28+
To distinguish between multiple processors of the same type, parameters set in the `measure(config, ...)`
29+
command will override these values. Additionally, any parameters set in `eventOptions` will override the other 2.
30+
Note that the order that you call config or set in does not matter - either order produces the same
31+
result.
32+
33+
```js
34+
// Set the default parameters.
35+
// Call the constructor with {}
36+
measure('config', 'googleAnalytics', {}, 'cookies', {});
37+
measure('set', 'googleAnalytics', {animal: 'dog', color: 'red'});
38+
// Call the constructor with {currency: 'USD', animal: 'cat', color: 'blue'}
39+
measure('config', 'googleAnalytics', {currency: 'USD', animal: 'cat', color: 'blue'}, 'cookies', {});
40+
// The first processor sees the eventOptions parameter as
41+
// {animal: 'dog', color: 'green'}
42+
// The second processor sees the eventOptions parameter as
43+
// {currency: 'USD', animal: 'cat', color: 'green'}
44+
measure('event', 'name', {color: 'green'});
45+
```
46+
So the set command will add arguments to both the constructor and every event, and the constructor
47+
will also add arguments to every event.
48+
49+
### Edge cases
50+
When overwriting data, values are shallow-copied, so the first value is overwritten in the next example
51+
```js
52+
// The default parameters are {options: {a: 1}}
53+
measure('set', 'googleAnalytics', {options: {a: 1}});
54+
// The default parameters are {options: {b: 2}}, not {options: {a: 1, b: 2}}
55+
measure('set', 'googleAnalytics', {options: {b:'2'}});
56+
```

docs/README.md

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,54 +67,3 @@ be saved for, and save it. To overwrite this behavior, you can specify a third t
6767
`measure('set', key, value, secondsToLive)`. In particular, if `secondsToLive` is 0, no data will be saved
6868
to long term storage.
6969

70-
The set command can also be used to set parameters related to a implementation of
71-
an event processor or storage interface. For example, to modify the default parameters
72-
of the cookies storage, run a `set` command in the script tag before calling
73-
`config`.
74-
75-
```js
76-
// Set the default cookie parameters
77-
measure('set', 'cookies', {prefix: 'my_', expires: 11});
78-
// Use the default cookie parameters: prefix 'my_' and expires 11.
79-
measure('config', 'eventProcessorName', {}, 'cookies', {});
80-
// Override a default cookie parameter: expires is 22 for this storage.
81-
measure('config', 'eventProcessorName', {}, 'cookies', {expires: 22});
82-
```
83-
84-
# Local Setup
85-
## Creating The Build
86-
You will need to have either [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
87-
or [yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) installed.
88-
89-
First, install dependencies with yarn or npm
90-
```shell script
91-
yarn install
92-
# or
93-
npm install
94-
```
95-
96-
Next, run the tests using yarn or npm:
97-
98-
```shell script
99-
yarn test
100-
# or
101-
npm run test
102-
```
103-
104-
## Running Tests Interactively
105-
You can also run the tests "interactively" via Karma directly.
106-
107-
```shell script
108-
yarn unit
109-
# or
110-
npm run unit
111-
```
112-
113-
To run the integration tests instead of the unit tests,
114-
115-
```shell script
116-
yarn integration
117-
# or
118-
npm run integration
119-
```
120-
****

docs/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
* [Home](/)
2+
* [PersistentParameters](PersistentParameters.md)
23
* Recommended Parameters
34
* [Pageview](Pageview.md)
45
* [Event Processors](EventProcessors.md)
56
* [Storage Interfaces](StorageInterfaces.md)
7+
* [Local Setup](LocalSetup.md)

src/config/configProcessors.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,41 @@ function buildProcessor_(constructor, params) {
8585
}
8686
}
8787

88+
/**
89+
* Perform a merge on the given list of objects. Any properties that both
90+
* the ith and jth (i less than j) objects both share will be overwritten
91+
* to have just the property of the jth object.
92+
*
93+
* @param {...} args The objects to merge.
94+
* @return {!Object<string, *>}
95+
*/
96+
const merge = function(args) {
97+
const result = {};
98+
for (let i = 0; i < arguments.length; i++) {
99+
const toMerge = arguments[i];
100+
for (const prop in toMerge) {
101+
if (toMerge.hasOwnProperty(prop)) {
102+
result[prop] = toMerge[prop];
103+
}
104+
}
105+
}
106+
return result;
107+
};
108+
109+
/**
110+
* Get extra parameters that are stored in the data layer for a given object.
111+
*
112+
* @param {?DataLayerHelper} helper
113+
* @param {?Object} object The object that we would like to get parameters of.
114+
* @return {!Object<string, *>}
115+
*/
116+
const getExtraOptions = (helper, object) => {
117+
if (helper && object && object.hasOwnProperty('getName')) {
118+
return /** @type {!Object<string, *>} */ (helper.get(object.getName()));
119+
}
120+
return {};
121+
};
122+
88123
/**
89124
* When called with an implementation of the eventProcessor and
90125
* storageInterface API, this function registers processors to react to the
@@ -153,6 +188,8 @@ function registerEventAndSet_(helper, processor, eventOptions, storage) {
153188
function processEvent(name, options = undefined) {
154189
const model = this;
155190
if (!options) options = {};
191+
options = merge(getExtraOptions(helper, processor.constructor),
192+
eventOptions, options);
156193
processor.processEvent(/** @type {!StorageInterface} */(storage),
157194
model, name, options);
158195
}

test/unit/config/buildProcessor_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const GoogleAnalyticsEventProcessor = goog.require('measurementLibrary.eventProc
77
describe('Calling the buildProcessor_ function of configProcessors', () => {
88
it('returns an event processor when a valid string is passed', () => {
99
expect(buildProcessor_('googleAnalytics', {})).toBeInstanceOf(
10-
GoogleAnalyticsEventProcessor);
10+
GoogleAnalyticsEventProcessor);
11+
});
12+
13+
it('returns an event processor when a class is passed in ', () => {
14+
expect(buildProcessor_(GoogleAnalyticsEventProcessor, {}))
15+
.toBeInstanceOf(GoogleAnalyticsEventProcessor);
1116
});
1217

13-
it('returns an event processor when a class is passed in ',
14-
() => {
15-
expect(buildProcessor_(GoogleAnalyticsEventProcessor, {}))
16-
.toBeInstanceOf(GoogleAnalyticsEventProcessor);
17-
});
1818
it('eeturns false when an invalid string is passed in ', () => {
1919
expect(buildProcessor_('unimplemented', {})).toEqual(null);
2020
});

0 commit comments

Comments
 (0)