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

feat(gatsby-plugin-google-analytics): add custom event helper #17612

Merged
merged 29 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8040a0f
Added CustomTrackingEvent function
robmarshall Sep 13, 2019
921d8de
Removed console log
robmarshall Sep 13, 2019
a7292a8
Documentation on how to use function
robmarshall Sep 13, 2019
50c4e4e
Added basic test
robmarshall Sep 13, 2019
4218195
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 16, 2019
dd42808
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 16, 2019
338fb63
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 16, 2019
b6b2e50
Removed spaces
robmarshall Sep 16, 2019
a63ae89
Update packages/gatsby-plugin-google-analytics/README.md
robmarshall Sep 16, 2019
640c633
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 17, 2019
a55bcb4
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 17, 2019
20a692c
Update packages/gatsby-plugin-google-analytics/README.md
robmarshall Sep 17, 2019
2d33997
Removed proptypes as function not a component
robmarshall Sep 17, 2019
b61a248
Added target and nonInteraction values
robmarshall Sep 17, 2019
65a3b8f
Added hitCallback to customTracker
robmarshall Sep 17, 2019
b95bc58
Updated readme with new customTracker details
robmarshall Sep 17, 2019
f3dc978
Updated value to "integer" rather than number
robmarshall Sep 17, 2019
0d6809f
Update packages/gatsby-plugin-google-analytics/src/index.js
robmarshall Sep 18, 2019
1ec34bf
Added createFunctionWithTimeout
robmarshall Sep 18, 2019
841ff0b
Corrected function export
robmarshall Sep 18, 2019
766a2dd
Added hitCallback timeout explaination
robmarshall Sep 18, 2019
f0b2d70
Update packages/gatsby-plugin-google-analytics/src/__tests__/index.js
robmarshall Sep 18, 2019
722e14b
Update packages/gatsby-plugin-google-analytics/src/__tests__/index.js
robmarshall Sep 18, 2019
e252b04
Update packages/gatsby-plugin-google-analytics/README.md
robmarshall Oct 11, 2019
8d21fc5
Update packages/gatsby-plugin-google-analytics/src/__tests__/index.js
robmarshall Oct 11, 2019
7d7ee6c
Update packages/gatsby-plugin-google-analytics/README.md
robmarshall Oct 11, 2019
5f3ca68
Update packages/gatsby-plugin-google-analytics/README.md
robmarshall Oct 11, 2019
6230f86
cleanup code & fix typedefs
wardpeet Nov 13, 2019
2d1bd25
Merge branch 'master' into topics/custom-analytics-events
wardpeet Nov 13, 2019
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
36 changes: 36 additions & 0 deletions packages/gatsby-plugin-google-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,39 @@ This plugin also supports several optional General fields documented in [Google
- `transport`: string

These fields can be specified in the plugin's `options` as shown in the [How to use](#how-to-use) section.

## CustomTrackingEvent Function
robmarshall marked this conversation as resolved.
Show resolved Hide resolved

To allow custom events to be tracked, the plugin exposes a function to include in your project.

To use it, simply import it and use it within your scripts, both within components and logic functions.
robmarshall marked this conversation as resolved.
Show resolved Hide resolved

```jsx
import React
import { CustomTrackingEvent } from 'gatsby-plugin-google-analytics'
robmarshall marked this conversation as resolved.
Show resolved Hide resolved

export default () => {
<div>
<button
onClick={e => {
// To stop the page reloading
e.preventDefault()
// Lets track that custom click
CustomTrackingEvent({
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
// string - required - The object that was interacted with (e.g.video)
category: "Special Button",
// string - required - Type of interaction (e.g. 'play')
action: "Click",
string - optional - Useful for categorizing events (e.g. 'Spring Campaign')
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
label: "Gatsby Plugin Example Campaign",
// number - optional - Numeric value associated with the event. (e.g. A product ID)
value: 43
})
//... Other logic here
}}
>
Tap that!
</button>
</div>
}
```
38 changes: 37 additions & 1 deletion packages/gatsby-plugin-google-analytics/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import { cleanup, fireEvent, render } from "@testing-library/react"
import { OutboundLink } from "../"
import { CustomTrackingEvent, OutboundLink } from "../"
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
robmarshall marked this conversation as resolved.
Show resolved Hide resolved

describe(`index.js`, () => {
describe(`<OutboundLink />`, () => {
Expand Down Expand Up @@ -65,4 +65,40 @@ describe(`index.js`, () => {
expect(props.onClick).toHaveBeenCalled()
})
})

describe(`CustomTrackingEvent()`, () => {
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
afterEach(cleanup)

const setup = props => {
const utils = render(
<button
onClick={e => {
e.preventDefault()
CustomTrackingEvent({
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
category: `event`,
action: `action`,
label: `label`,
value: `value`,
})
}}
>
tapthis
</button>
)

return Object.assign({}, utils, {
button: utils.getByText(`tapthis`),
})
}

it(`sends tracking event when clicked`, () => {
window.ga = jest.fn()

const { button } = setup()

fireEvent.click(button)

expect(window.ga).toHaveBeenCalled()
})
})
})
29 changes: 28 additions & 1 deletion packages/gatsby-plugin-google-analytics/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,31 @@ OutboundLink.propTypes = {
onClick: PropTypes.func,
}

export { OutboundLink }
/**
* This allows the user to create custom events within their Gatsby projects.
* @param {string} category Required - The object that was interacted with (e.g.video)
* @param {string} action Required - Type of interaction (e.g. 'play')
* @param {string} label Optional - Useful for categorizing events (e.g. 'Spring Campaign')
* @param {string} value Optional - Numeric value associated with the event. (e.g. A product ID)
*
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#events
*/
function CustomTrackingEvent({ category, action, label, value }) {
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
if (window.ga) {
robmarshall marked this conversation as resolved.
Show resolved Hide resolved
window.ga(`send`, `event`, {
eventCategory: category,
eventAction: action,
eventLabel: label,
eventValue: value,
})
}
}

CustomTrackingEvent.propTypes = {
category: PropTypes.string.isRequired,
action: PropTypes.string.isRequired,
label: PropTypes.string,
value: PropTypes.number,
}
robmarshall marked this conversation as resolved.
Show resolved Hide resolved

export { OutboundLink, CustomTrackingEvent }