Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from teambition/feature/tracking-actions
Browse files Browse the repository at this point in the history
add data-tbtracking support
  • Loading branch information
nekolab authored Oct 16, 2019
2 parents e4fa3a2 + 0585812 commit 2b5cac3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Then, include the following script in your HTML and you are ready to go:
data-google="UA-3318xxxx-1"
data-mixpanel="77e13d08ba42fe31932a1f1418aea7b2"
data-customer="2ac3fd02efd1f9c57ae9"
data-tbtracking="your actions path"
></script>
```

Expand Down Expand Up @@ -89,11 +90,36 @@ You can call the `event` function to track an event:
gta.event({action: 'add content', page: 'Project Page', type: 'task', control: 'tasks layout', method: 'double-click'})
```

Or, easily add `data-gta='event'` to a DOM element as:
either add `data-gta='event'` to a DOM element as:
```html
<button data-gta="{action: 'add content', page: 'Project Page', type: 'task', control: 'tasks layout', method: 'double-click'}">click</button>
```

or preloading actions to format as:

1. `data-gta-hash` property in DOM element use to load actions.
```html
<script id="gta-main"
...
data-tbtracking="your actions path"
></script>
```
2. `hash` is the target in your acions list.

```html
actions = [
...,
{
hash: hash
action: 'add content',
control: 'tasks layout',
type: 'task',
}
]

<button data-gta-hash="${hash}">click</button>
```

To automatically log gtaOptions, you can use the 'debug' mode:
```js
gta.debug = true
Expand All @@ -113,6 +139,10 @@ window._gta_debug = true
* [SensorsData](https://www.sensorsdata.cn/manual/js_sdk.html)

## Change Log

#### 1.1.4
1. Add `data-tbtracking` support

#### 1.1.1 - 1.1.2
1. New Plugin: `distinct id`

Expand Down
61 changes: 59 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ return /******/ (function(modules) { // webpackBootstrap

GTA.prototype.mixPayload = {};

GTA.prototype.version = '1.1.3';
GTA.prototype.actionMap = {};

GTA.prototype.version = '1.1.4';

function GTA() {
var $el;
Expand All @@ -84,6 +86,7 @@ return /******/ (function(modules) { // webpackBootstrap
return;
}
this.delegateEvents();
this.preloadActions($el);
}

GTA.prototype.init = function() {
Expand All @@ -108,6 +111,37 @@ return /******/ (function(modules) { // webpackBootstrap
return Common.removeElement($el);
};

GTA.prototype.initXhr = function(url) {
var xhr;
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
return xhr;
};

GTA.prototype.preloadActions = function($el) {
var url, xhr;
url = $el.getAttribute('data-tbtracking');
if (!url) {
return;
}
xhr = this.initXhr(url);
return xhr.onreadystatechange = (function(_this) {
return function() {
var actions, i, item, len, results;
if (xhr.readyState === 4 && xhr.status === 200) {
actions = JSON.parse(xhr.response);
results = [];
for (i = 0, len = actions.length; i < len; i++) {
item = actions[i];
results.push(_this.actionMap[item.hash] = (_this.actionMap[item.hash] || []).concat(item));
}
return results;
}
};
})(this);
};

GTA.prototype.registerProperty = function(key, value) {
this.mixPayload[key] = value;
return this;
Expand Down Expand Up @@ -246,16 +280,24 @@ return /******/ (function(modules) { // webpackBootstrap
listener = (function(_this) {
return function(e) {
return window.setTimeout(function() {
var el, gtaIgnore, gtaOptions, gtaString, ref, ref1, results;
var action, actions, el, gtaHash, gtaIgnore, gtaOptions, gtaString, i, len, ref, ref1, ref2, results;
el = e.target;
results = [];
while (el) {
gtaString = (ref = el.dataset) != null ? ref.gta : void 0;
gtaIgnore = (ref1 = el.dataset) != null ? ref1.gtaIgnore : void 0;
gtaOptions = Common.parseGta(gtaString);
gtaHash = (ref2 = el.dataset) != null ? ref2.gtaHash : void 0;
if (gtaOptions && (!gtaIgnore || matches(e.target, gtaIgnore))) {
_this.event(gtaOptions);
}
if (gtaHash) {
actions = _this.actionMap[gtaHash] || [];
for (i = 0, len = actions.length; i < len; i++) {
action = actions[i];
_this.event(Common.pick(action, ['action', 'type', 'control']));
}
}
results.push(el = el.parentElement);
}
return results;
Expand Down Expand Up @@ -344,6 +386,21 @@ return /******/ (function(modules) { // webpackBootstrap
return result;
};

Common.pick = function(obj, keys) {
var i, key, len, result;
if (!obj) {
return;
}
result = {};
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i];
if (obj[key]) {
result[key] = obj[key];
}
}
return result;
};


/***/ },
/* 2 */
Expand Down
8 changes: 8 additions & 0 deletions src/common.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ Common.formatUser = (provider, user)->
else
result[key] = value
return result

Common.pick = (obj, keys) ->
return unless obj
result = {}
for key in keys
if obj[key]
result[key] = obj[key]
return result
28 changes: 27 additions & 1 deletion src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class GTA
plugins: []
providers: []
mixPayload: {}
actionMap: {}

version: '1.1.3'
version: '1.1.4'

constructor: ->
return if typeof document is 'undefined'
$el = document.getElementById 'gta-main'
return unless $el
@delegateEvents()
@preloadActions($el)

init: ->
return if typeof document is 'undefined'
Expand All @@ -29,6 +31,22 @@ class GTA

Common.removeElement $el

initXhr: (url) ->
xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.send()
return xhr

preloadActions: ($el) ->
url = $el.getAttribute 'data-tbtracking'
return unless url
xhr = @initXhr(url)
xhr.onreadystatechange = () =>
if xhr.readyState is 4 and xhr.status is 200
actions = JSON.parse(xhr.response)
for item in actions
@actionMap[item.hash] = (@actionMap[item.hash] or []).concat(item)

registerProperty: (key, value)->
@mixPayload[key] = value
return this
Expand Down Expand Up @@ -114,8 +132,16 @@ class GTA
gtaString = el.dataset?.gta
gtaIgnore = el.dataset?.gtaIgnore
gtaOptions = Common.parseGta gtaString
gtaHash = el.dataset?.gtaHash

if gtaOptions and (!gtaIgnore or matches(e.target, gtaIgnore))
@event gtaOptions

if gtaHash
actions = @actionMap[gtaHash] or []
for action in actions
@event Common.pick action, ['action', 'type', 'control']

el = el.parentElement
, 0
document.body.addEventListener 'click', listener, true
Expand Down

0 comments on commit 2b5cac3

Please sign in to comment.