Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom click properties #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ Properties
- text - `View Account`
- href - `/account`

Setting custom properties with `data-ahoy-click-` attributes:

```html
<a href="/my_product" data-ahoy-click-product-id="123" data-ahoy-click-cat="456">Extra</a>
```

Properties

- tag - `a`
- text - `Extra`
- href - `/my_product`
- properties - `{product_id: "123", cat: "456"}`

Setting custom properties from JSON with `data-ahoy-click-json` attribute:

```html
<a href="/my_product" data-ahoy-click-json="">Extra JSON</a>
```

<a data-ahoy-click-json="{&quot;some_flag&quot;:true,&quot;some_count&quot;:42}" href="/link">JSON</a>

Properties

- tag - `a`
- text - `JSON`
- href - `/link1`
- properties - `{some_flag: true, some_count: 42}`

JSON allows typing and nesting extra properties, but requires HTML escaping. HTML escaping is automatic in many templating systems like ERB.

### Submits

```javascript
Expand Down
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ function createVisit() {
}
}

function extraClickProperties(element) {
let prefix = "data-ahoy-click-";
let jsonAttribute = prefix + 'json';

if (element.hasAttribute(jsonAttribute)) {
return JSON.parse(element.getAttribute(jsonAttribute));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to combine if both are set.

}

let properties = {};

let attributeNames = element.getAttributeNames();
for (let i = 0; i < attributeNames.length; i++) {
let attributeName = attributeNames[i];
if (attributeName.startsWith(prefix)) {
let propertyName = attributeName.substring(prefix.length).replace("-", "_");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

properties[propertyName] = element.getAttribute(attributeName);
}
}

return properties;
}

ahoy.getVisitId = ahoy.getVisitToken = function () {
return getCookie("ahoy_visit");
};
Expand Down Expand Up @@ -419,6 +441,11 @@ ahoy.trackClicks = function () {
let properties = eventProperties(e);
properties.text = properties.tag == "input" ? target.value : (target.textContent || target.innerText || target.innerHTML).replace(/[\s\r\n]+/g, " ").trim();
properties.href = target.href;

let extraProperties = extraClickProperties(target);
for (var propName in extraProperties) {
properties[propName] = extraProperties[propName];
}
ahoy.track("$click", properties);
});
};
Expand Down
30 changes: 28 additions & 2 deletions test/ahoy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ test('View tracking', (t) => {
});

test('Click tracking', (t) => {
t.plan(6);
t.plan(7);

fauxJax.install();
fauxJax.once('request', function(request) {
const event = JSON.parse(request.requestBody).events[0];
const properties = event.properties
const properties = event.properties;

t.equal(request.requestMethod, 'POST', 'Should use POST method');
t.equal(request.requestURL, '/ahoy/events', 'Should POST to correct URL');
t.equal(event.name, '$click', 'Should set event name property');
t.equal(properties.id, 'ahoy-test-link', 'Should set event id property');
t.equal(properties.section, 'Header', 'Should set event section property');
t.equal(properties.text, 'Home', 'Should set event text property');
t.equal(properties.properties, undefined, 'Should not set extra properties');

request.respond(200, {}, '{}');
fauxJax.restore();
Expand All @@ -105,3 +106,28 @@ test('Click tracking', (t) => {
ahoy.trackClicks();
document.getElementById('ahoy-test-link').click();
});

test('Click tracking extra properties', (t) => {
t.plan(8);

fauxJax.install();
fauxJax.once('request', function(request) {
const event = JSON.parse(request.requestBody).events[0];
const properties = event.properties;

t.equal(request.requestMethod, 'POST', 'Should use POST method');
t.equal(request.requestURL, '/ahoy/events', 'Should POST to correct URL');
t.equal(event.name, '$click', 'Should set event name property');
t.equal(properties.id, 'ahoy-test-link-extra', 'Should set event id property');
t.equal(properties.section, 'Header', 'Should set event section property');
t.equal(properties.text, 'Extra', 'Should set event text property');
t.equal(properties.properties.foo, 'bar', 'Should set extra property "foo"');
t.equal(properties.properties.the_answer, '42', 'Should set extra property "the_answer"');

request.respond(200, {}, '{}');
fauxJax.restore();
});

ahoy.trackClicks();
document.getElementById('ahoy-test-link-extra').click();
});
4 changes: 4 additions & 0 deletions test/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

<div data-section="Header" style="display: none;">
<a id="ahoy-test-link" href="javascript:void(0)">Home</a>
<a id="ahoy-test-link-extra"
href="javascript:void(0)"
data-ahoy-click-foo="bar"
data-ahoy-click-the_answer="42">Extra</a>
</div>