2021-04-15 17:06:11 +02:00
import { CleanInsights , ConsentRequestUi } from 'clean-insights-sdk' ;
export default {
2022-06-30 08:38:58 +00:00
install ( Vue , config ) {
2021-04-15 17:06:11 +02:00
class RequestUi extends ConsentRequestUi {
showForCampaign ( campaignId , campaign , complete ) {
2021-09-25 09:29:05 +02:00
const period = campaign . nextTotalMeasurementPeriod
2021-04-15 17:06:11 +02:00
if ( ! period ) {
return ''
}
let message = 'Help us improve!\n\n'
+ ` We would like to collect anonymous usage data between ${ period . start . format ( 'LLL' ) } and ${ period . end . format ( 'LLL' ) } to improve the quality of the product. \n \n Your help would be highly appreciated. `
complete ( window . confirm ( message ) )
return ''
}
}
const cleanInsightsService = new Vue ( {
2022-06-30 08:38:58 +00:00
config : config ,
2021-04-15 17:06:11 +02:00
data ( ) {
return {
ci : null ,
campaignId : null ,
requestUi : null ,
}
} ,
created ( ) {
2022-06-30 08:38:58 +00:00
if ( this . $options . config ) {
const config = this . $options . config ;
this . ci = new CleanInsights ( config ) ;
2021-04-15 17:06:11 +02:00
2022-06-30 08:38:58 +00:00
// Get name of first campaign in the config.
this . campaignId = Object . keys ( config . campaigns || { invalid : { } } ) [ 0 ] ;
2021-09-25 09:29:05 +02:00
2022-06-30 08:38:58 +00:00
}
2021-04-15 17:06:11 +02:00
} ,
methods : {
event ( category , action ) {
if ( ! this . ci ) {
return ;
}
if ( ! this . requestUi ) {
this . requestUi = new RequestUi ( ) ;
}
this . ci . requestConsentForCampaign ( this . campaignId , this . requestUi , ( granted ) => {
if ( ! granted ) {
return
}
this . ci . measureEvent ( category , action , this . campaignId ) ;
} )
}
}
} ) ;
2022-06-30 08:38:58 +00:00
return cleanInsightsService ;
2021-04-15 17:06:11 +02:00
}
}