Skip to content
Open

V4 #37

Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d7c0798
updating readme
Psiale May 4, 2022
eb1f6b0
working on G4 updates
Psiale May 13, 2022
80be535
added sendEventG4
Psiale May 13, 2022
80e631f
created sendEventG4
Psiale May 13, 2022
2455598
changed README
Psiale May 13, 2022
9736ceb
fixed readme
Psiale May 13, 2022
4555ddb
workin on example
Psiale May 13, 2022
fdd2326
adding example
Psiale May 13, 2022
dbb2a98
handling error
Psiale May 13, 2022
4a0ffaf
handling error
Psiale May 14, 2022
3db9b49
handling error
Psiale May 14, 2022
b19b96a
error handling
Psiale May 16, 2022
5bec458
solved error
Psiale May 16, 2022
2d0c902
console log args"
Psiale May 16, 2022
81676a9
debugging
Psiale May 17, 2022
217b1be
debugging
Psiale May 17, 2022
571e30b
debugging
Psiale May 17, 2022
c784013
debugging
Psiale May 17, 2022
1ce074e
debugging
Psiale May 17, 2022
834364e
debugging
Psiale May 17, 2022
a43f8ab
adding client_id
Psiale May 17, 2022
cbcb6ea
using gtag method)
Psiale May 23, 2022
8944ea5
removing unnecesary check for GTAg
Psiale May 23, 2022
068c923
working
Psiale May 23, 2022
b35ebf9
working this out
Psiale May 23, 2022
a9ab204
working this out
Psiale May 23, 2022
4a8c240
changed sendEvent method to also use V4
Psiale May 23, 2022
76626e6
Update README.md
Psiale May 24, 2022
a982b21
done with requested changes
Psiale May 24, 2022
b0d92e0
Merge branch 'v4' of github.com:firstandthird/ga-track into v4
Psiale May 24, 2022
d4a6a37
working on GA using dataLayer
Psiale Jun 1, 2022
6a00989
working on GA using dataLayer
Psiale Jun 1, 2022
0186efd
working on GA using dataLayer
Psiale Jun 1, 2022
77c90dd
working on GA using dataLayer
Psiale Jun 1, 2022
1d8b387
working on GA using dataLayer
Psiale Jun 1, 2022
7496b1d
working on GA using dataLayer
Psiale Jun 1, 2022
87ea009
working on GA using dataLayer
Psiale Jun 1, 2022
af11a59
working on GA using dataLayer
Psiale Jun 2, 2022
2fe291f
working on GA using dataLayer
Psiale Jun 2, 2022
dfa7e73
working on GA using dataLayer
Psiale Jun 2, 2022
e6eb5b3
working on GA using dataLayer
Psiale Jun 2, 2022
097ca19
changing object structure
Psiale Jun 2, 2022
779cb9e
changed sendEvent and created new function
Psiale May 15, 2023
19744a5
adding ga-track
Psiale May 19, 2023
eac406e
solving conflicts
Psiale May 22, 2023
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
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ import GATrack from 'ga-track';

## Methods

### sendEvent(category, action, label)

Manually sends an event to Google Analytics. Returns a promise.
### sendEvent(category,action, label)
Manually sends an event to Google Analytics. Returns a promise.;

#### Parameters:

`category` - {string} - Event's category.

`action` - {string} - Event's action.

`label` - {string} - Event's label.

### sendEventV4(name, event_params)

`name` - {string}

`event_params` - {Array of strings} - custom or [recommend](https://support.google.com/analytics/answer/9267735) event params ([25 max](https://support.google.com/analytics/answer/9267744?hl=en)).
V4
```javascript
GaTrack.G4 = true;
```
```javascript
GATrack.sendEventV4('read_article',[{name: 'author', value: 'David Mitchell'}, {name: 'title', value: 'Cloud Atlas'}]);
```

### sendData()

Safely pass data to Google Analytics:
Expand All @@ -53,6 +64,14 @@ GATrack.sendData('set', 'dimension2', 'member');

## Options

### V4
```js
import GATrack from 'ga-track';

GATrack.V4 = true;
```
Allows you to use the [V4](#sendeventv4) version of sendEvent()

### Debug Mode

```js
Expand Down
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', ' G-88YEW4V4VV');
</script>
</head>
<body>
<button onclick="test()">Click me to run test</button>
<script src="index.js">
GaTrack.V4 = true;
const test = () => GaTrack.sendEventV4('test', [{test_param_1: 'test value 1'}, {test_param_2: 'test value 2'}])
</script>
</body>
</html>
69 changes: 65 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,71 @@ class GATrack {
static debug = false;
static trackerName = '';
static force = null;
static V4 = false;

static async sendEventV4(event_name, event_params) {
if (GATrack.V4 === false) {
console.error('to use sendEventV4 change GATrack.V4 to true')
return;
}

if (typeof event_name !== 'string') {
console.error("event_name has to be of type string");
return;
}

if(event_name === '' || event_name === null) {
console.error("event name is required");
return;
}

if (event_params.length > 25) {
console.error("can't send more than 25 event params")
return;
}


if(GATrack.V4) {
const payload = {
events:[{
name: event_name,
params: event_params
}],
}

return new Promise(resolve => {
this.log(payload);
this.log(this.isEnabled());
if (!this.isEnabled()) {
this.log('sendEventV4', 'ga-track disabled');
return resolve();
}
this.sendData(payload);
})
}
return;
}

static async sendEvent(category, action, label) {
if (this.prefix) {
category = `${this.prefix}-${category}`;
}

return new Promise(resolve => {
let payload = {}
if (this.V4) {
payload = {
events:[{
name: action,
params: {
event_category: category,
event_label: label
}
}],
}
this.sendData(payload);
return;
}
this.log(category, action, label);

if (!this.isEnabled()) {
Expand All @@ -24,7 +82,7 @@ class GATrack {
window._gaq.push(resolve);
} else if (this.isGTag()) {
// Gtag check needs to go before since gtag creates a ga variable
const payload = {
payload = {
event_category: category,
event_label: label,
event_callback: resolve
Expand Down Expand Up @@ -57,13 +115,16 @@ class GATrack {
return;
}

if (this.isGTag()) {
window.gtag.apply(null, args);
if (this.V4) {
setTimeout(() => {
gtag("event", `${args[0].events.name}`, args[0].events.params);
return;
}, 3000);
} else if (this.isGA()) {
if (this.trackerName) {
args[0] = `${this.trackerName}.${args[0]}`;
}

console.log(window.ga.apply(null, args));
window.ga.apply(null, args);
}
}
Expand Down