-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more logical to have it there given companion is responsible for interacting with GA4 api to begin with. It also simplifies some of the logic around passing around measurementID and secret
- Loading branch information
Showing
4 changed files
with
121 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,64 @@ | ||
import { me as appbit } from 'appbit' | ||
import { me as device } from 'device' | ||
import { display } from 'display' | ||
import { encode } from 'cbor' | ||
import { outbox } from 'file-transfer' | ||
import { readFileSync, writeFileSync } from 'fs' | ||
import shared from './shared' | ||
|
||
//==================================================================================================== | ||
// Configure | ||
//==================================================================================================== | ||
|
||
let mesaurementId = null | ||
let apiSecret = null | ||
let debug = false | ||
let configured = false | ||
|
||
// Update global options | ||
const configure = options => { | ||
if (!options) { | ||
return | ||
} | ||
|
||
mesaurementId = options.mesaurementId | ||
apiSecret = options.apiSecret | ||
debug = options.debug || debug | ||
// TODO user_properties | ||
|
||
if (!mesaurementId || !apiSecret) { | ||
console.log('GA4 configure: no measurement ID or API secret provided, no events will be sent.') | ||
return | ||
} | ||
|
||
configured = true | ||
onload() | ||
const setDebug = value => { | ||
debug = !!value | ||
} | ||
|
||
//==================================================================================================== | ||
// Send | ||
// Can be a single event object, or array of events | ||
//==================================================================================================== | ||
const send = event => { | ||
if (!configured) { | ||
console.log('GA4 send: GA4 not configured, dropping event') | ||
return | ||
} | ||
|
||
const data = shared.transformData(event) | ||
data.measurementId = mesaurementId | ||
data.apiSecret = apiSecret | ||
data.debug = debug | ||
|
||
// Generate a unique filename | ||
const filename = '_google_analytics_' + (Math.floor(Math.random() * 10000000000000000)) | ||
const filename = '_google_analytics4_' + (Math.floor(Math.random() * 10000000000000000)) | ||
// Enqueue the file | ||
outbox.enqueue(filename, encode(data)).then(() => { | ||
debug && console.log('File: ' + filename + ' transferred successfully.') | ||
debug && console.log(`File: ${filename} transferred successfully.`) | ||
}).catch(function (error) { | ||
debug && console.log('File: ' + filename + ' failed to transfer.') | ||
debug && console.log(`File: ${filename} failed to transfer.`) | ||
}) | ||
} | ||
|
||
//==================================================================================================== | ||
// Automatic Events | ||
//==================================================================================================== | ||
// If invoking, ensure you're invoking at the top of your 'app' right after importing the module. | ||
const sendLoadAndDisplayOnEvents = value => { | ||
if (!!value === false) { | ||
return | ||
} | ||
|
||
// Send a hit on load | ||
const onload = () => { | ||
// Send an event on load | ||
send({ | ||
name: 'load', | ||
}) | ||
} | ||
|
||
// Send a hit each time the display turns on | ||
display.addEventListener('change', () => { | ||
send({ | ||
name: 'display', | ||
params: { | ||
value: display.on ? 'on' : 'off', | ||
}, | ||
// Send an event each time the display turns on | ||
display.addEventListener('change', () => { | ||
if (display.on) { | ||
send({ | ||
name: 'display_on', | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
// Send a hit on unload | ||
appbit.addEventListener('unload', () => { | ||
send({ | ||
name: 'unload', | ||
// Send an event on unload | ||
appbit.addEventListener('unload', () => { | ||
send({ | ||
name: 'unload', | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
const analytics = { | ||
configure, | ||
sendLoadAndDisplayOnEvents, | ||
send, | ||
setDebug, | ||
} | ||
|
||
export default analytics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters