Seznam IMA.js google analytic plugin
npm install @ima/plugin-analytic-googleThis is the Google analytic plugin for the IMA.js application. You can visit our site
The plugin currently implements GA4 analytics.
``console`
npm install @ima/plugin-analytic-google @ima/plugin-script-loader --save
`javascript
// /app/build.js
var vendors = {
common: [
'@ima/plugin-analytic-google',
'@ima/plugin-analytic',
'@ima/plugin-script-loader'
]
};
/*
Now is google analytic plugin available from:
ns.ima.plugin.analytic.google.GoogleAnalytics4;
ns.ima.plugin.analytic.google.googleAnalytics4DefaultDependencies;
import { GoogleAnalytics4, googleAnalytics4DefaultDependencies } from '@ima/plugin-analytic-google';
*/
`
`javascript
// /app/config/settings.js
prod: {
$Http: { ... },
$Cache: { ... },
$Page:{ ... },
plugin : {
analytic: {
google4: { //for GA4
service: 'G-XXXXXXXXXX'
}
}
}
}
`
`javascript
// /app/config/services.js
import { GoogleAnalytics4 } from '@ima/plugin-analytic-google';
import { RouterEvents } from '@ima/core';
var $window = oc.get('$Window');
var $dispatcher = oc.get('$Dispatcher');
var googleAnalytics4 = oc.get(GoogleAnalytics4);
if ($window.isClient()) {
// get info about purpose consents of your user, see part Handling user's purposeConsents
const purposeConsents = getPurposeConsents();
// insert analytic script to page and initialization analytic
googleAnalytics4.init(purposeConsents);
// Since version 1.0 you need to additionally call a load() method. The later you call this method the better.load()
// If you don't have a specific point in your app where you know that the page has finished loading you can call the method immediatelly after init() method
googleAnalytics4.load();
//set hit page view to analytic
$dispatcher.listen(RouterEvents.AFTER_HANDLE_ROUTE, (pageData) => {
if (pageData &&
pageData.response &&
(pageData.response.status >= 200 &&
pageData.response.status < 300)) {
googleAnalytics4.hitPageView(pageData);
} else {
// hit error to google analytic
var label = pageData.params.error ? pageData.params.error.toString() : undefined;
var value = pageData.response.status ? pageData.response.status : undefined;
googleAnalytic.hit({
category: 'error',
action: 'render',
label,
value
});
}
});
}
`
object with permission granted is { '1': true }.
GA4 comes with possibility to change purpose consents at any time after initialisation by method updateConsent`.