Skip to content

Commit

Permalink
Add missing declarative attribute support
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Jun 30, 2016
1 parent 723cb4e commit 4cfa215
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 16 deletions.
2 changes: 1 addition & 1 deletion autotrack.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion autotrack.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/plugins/event-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ The following table outlines all possible configuration options for the `eventTr
<strong>Default:</strong> <code>['click']</code>
</td>
</tr>
<tr valign="top">
<td><code>fieldsObj</code></a></td>
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>attributePrefix</code></a></td>
<td><code>string</code></a></td>
Expand All @@ -50,11 +55,6 @@ The following table outlines all possible configuration options for the `eventTr
<strong>Default:</strong> <code>'ga-'</code>
</td>
</tr>
<tr valign="top">
<td><code>fieldsObj</code></a></td>
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>hitFilter</code></a></td>
<td><code>Function</code></a></td>
Expand Down
10 changes: 5 additions & 5 deletions docs/plugins/impression-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ The following table outlines all possible configuration options for the `impress
<td><code>string</code></a></td>
<td>This option is a CSS margin string accepting pixel or percentage values only. It is passed as the <a href="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver#Properties"><code>rootMargin</code></a> option to the <code>IntersectionObserver</code> instance, which is used to expand or contract the viewport area to change when an element is considered visible. For example: the string <code>'-20px'</code> would contract the viewport by 20 pixels on each side, and all element visibility calculations would be based on that rather than the viewport dimensions.</td>
</tr>
<tr valign="top">
<td><code>fieldsObj</code></a></td>
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>attributePrefix</code></a></td>
<td><code>string</code></a></td>
Expand All @@ -57,11 +62,6 @@ The following table outlines all possible configuration options for the `impress
<strong>Default:</strong> <code>'ga-'</code>
</td>
</tr>
<tr valign="top">
<td><code>fieldsObj</code></a></td>
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>hitFilter</code></a></td>
<td><code>Function</code></a></td>
Expand Down
8 changes: 8 additions & 0 deletions docs/plugins/outbound-form-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ The following table outlines all possible configuration options for the `outboun
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>attributePrefix</code></a></td>
<td><code>string</code></a></td>
<td>
See the <a href="/docs/common-options.md#attributeprefix">common options guide</a> for <code>attributePrefix</code> description.<br>
<strong>Default:</strong> <code>'ga-'</code>
</td>
</tr>
<tr valign="top">
<td><code>hitFilter</code></a></td>
<td><code>Function</code></a></td>
Expand Down
8 changes: 8 additions & 0 deletions docs/plugins/outbound-link-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ The following table outlines all possible configuration options for the `outboun
<td><code>Object</code></a></td>
<td>See the <a href="/docs/common-options.md#fieldsobj">common options guide</a> for <code>fieldsObj</code> description.</td>
</tr>
<tr valign="top">
<td><code>attributePrefix</code></a></td>
<td><code>string</code></a></td>
<td>
See the <a href="/docs/common-options.md#attributeprefix">common options guide</a> for <code>attributePrefix</code> description.<br>
<strong>Default:</strong> <code>'ga-'</code>
</td>
</tr>
<tr valign="top">
<td><code>hitFilter</code></a></td>
<td><code>Function</code></a></td>
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/event-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function EventTracker(tracker, opts) {

this.opts = assign({
events: ['click'],
attributePrefix: 'ga-',
fieldsObj: {},
attributePrefix: 'ga-',
hitFilter: null
}, opts);

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/impression-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function ImpressionTracker(tracker, opts) {
this.opts = assign({
elements: [],
rootMargin: '0px',
attributePrefix: 'ga-',
fieldsObj: {},
attributePrefix: 'ga-',
hitFilter: null
}, opts);

Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/outbound-form-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function OutboundFormTracker(tracker, opts) {
formSelector: 'form',
shouldTrackOutboundForm: this.shouldTrackOutboundForm,
fieldsObj: {},
attributePrefix: 'ga-',
hitFilter: null
}, opts);

Expand Down Expand Up @@ -79,7 +80,8 @@ OutboundFormTracker.prototype.handleFormSubmits = function(event, form) {
});
}

var userFields = assign({}, this.opts.fieldsObj, getAttributeFields(form));
var userFields = assign({}, this.opts.fieldsObj,
getAttributeFields(form, this.opts.attributePrefix));

this.tracker.send('event', createFieldsObj(
defaultFields, userFields, this.tracker, this.opts.hitFilter, form));
Expand Down
5 changes: 4 additions & 1 deletion lib/plugins/outbound-link-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function OutboundLinkTracker(tracker, opts) {
linkSelector: 'a',
shouldTrackOutboundLink: this.shouldTrackOutboundLink,
fieldsObj: {},
attributePrefix: 'ga-',
hitFilter: null
}, opts);

Expand Down Expand Up @@ -79,7 +80,9 @@ OutboundLinkTracker.prototype.handleLinkInteractions = function(event, link) {
eventAction: event.type,
eventLabel: link.href
};
var userFields = assign({}, this.opts.fieldsObj, getAttributeFields(link));

var userFields = assign({}, this.opts.fieldsObj,
getAttributeFields(link, this.opts.attributePrefix));

this.tracker.send('event', createFieldsObj(
defaultFields, userFields, this.tracker, this.opts.hitFilter, link));
Expand Down
14 changes: 14 additions & 0 deletions test/outbound-form-tracker.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
<input id="action-less-submit" type="submit">
</form>

<form
action="https://www.google-analytics.com/collect"
ga-event-category="External Form"
ga-dimension-1="true">
<input id="declarative-attributes-submit" type="submit">
</form>

<form
action="https://www.google-analytics.com/collect"
data-ga-event-label="www.google-analytics.com"
data-ga-non-interaction="true">
<input id="declarative-attributes-prefix-submit" type="submit">
</form>

<div id="shadow-host"></div>

<script>
Expand Down
36 changes: 36 additions & 0 deletions test/outbound-form-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,42 @@ describe('outboundFormTracker', function() {
});


it('supports setting attributes declaratively', function() {

var hitData = browser
.execute(utilities.stopSubmitEvents)
.execute(utilities.stubBeacon)
.execute(ga.run, 'require', 'outboundFormTracker')
.click('#declarative-attributes-submit')
.execute(ga.getHitData)
.value;

assert.equal(hitData.length, 1);
assert.equal(hitData[0].eventCategory, 'External Form');
assert.equal(hitData[0].eventAction, 'submit');
assert.equal(hitData[0].dimension1, true);
});


it('supports customizing the attribute prefix', function() {

var hitData = browser
.execute(utilities.stopSubmitEvents)
.execute(utilities.stubBeacon)
.execute(ga.run, 'require', 'outboundFormTracker', {
attributePrefix: 'data-ga-'
})
.click('#declarative-attributes-prefix-submit')
.execute(ga.getHitData)
.value;

assert.equal(hitData.length, 1);
assert.equal(hitData[0].eventAction, 'submit');
assert.equal(hitData[0].eventLabel, 'www.google-analytics.com');
assert.equal(hitData[0].nonInteraction, true);
});


it('should support specifying a hit filter', function() {

var hitData = browser
Expand Down
14 changes: 14 additions & 0 deletions test/outbound-link-tracker.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@

<a id="no-href-attribute">No <code>href</code> Attribute Link</a>

<a id="declarative-attributes"
href="https://www.google-analytics.com/collect"
ga-event-category="External Link"
ga-dimension-1="true">
Declarative Attributes
</a>

<a id="declarative-attributes-prefix"
href="https://www.google-analytics.com/collect"
data-ga-event-label="www.google-analytics.com"
data-ga-non-interaction="true">
Declarative Attributes
</a>

<div id="shadow-host"></div>

<script>
Expand Down
36 changes: 36 additions & 0 deletions test/outbound-link-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,42 @@ describe('outboundLinkTracker', function() {
});


it('supports setting attributes declaratively', function() {

var hitData = browser
.execute(utilities.stopClickEvents)
.execute(utilities.stubBeacon)
.execute(ga.run, 'require', 'outboundLinkTracker')
.click('#declarative-attributes')
.execute(ga.getHitData)
.value;

assert.equal(hitData.length, 1);
assert.equal(hitData[0].eventCategory, 'External Link');
assert.equal(hitData[0].eventAction, 'click');
assert.equal(hitData[0].dimension1, true);
});


it('supports customizing the attribute prefix', function() {

var hitData = browser
.execute(utilities.stopClickEvents)
.execute(utilities.stubBeacon)
.execute(ga.run, 'require', 'outboundLinkTracker', {
attributePrefix: 'data-ga-'
})
.click('#declarative-attributes-prefix')
.execute(ga.getHitData)
.value;

assert.equal(hitData.length, 1);
assert.equal(hitData[0].eventAction, 'click');
assert.equal(hitData[0].eventLabel, 'www.google-analytics.com');
assert.equal(hitData[0].nonInteraction, true);
});


it('should support specifying a hit filter', function() {

var hitData = browser
Expand Down

0 comments on commit 4cfa215

Please sign in to comment.