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: Yandex.Metrica #542

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ Specify locales for i18n support. For more details, see the guide on [Internatio

A function to control what files should have `<link rel="preload">` resource hints generated. See [shouldPrefetch](https://ssr.vuejs.org/api/#shouldprefetch).

### ym

- Type: `number`
- Default: `undefined`

Provide the Yandex.Metrica to enable integration.

## Theming

### theme
Expand Down
36 changes: 35 additions & 1 deletion lib/app/clientEntry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global BASE_URL, GA_ID, ga, SW_ENABLED */
/* global BASE_URL, GA_ID, ga, YM_ID, Ya SW_ENABLED */

import { createApp } from './app'
import { register } from 'register-service-worker'
Expand Down Expand Up @@ -29,6 +29,40 @@ if (process.env.NODE_ENV === 'production' && GA_ID) {
})
}

// Yandex.Metrica integration
if (process.env.NODE_ENV === 'production' && YM_ID) {
(function (d, w, c) {
(w[c] = w[c] || []).push(function () {
try {
w['yaCounter' + YM_ID] = new Ya.Metrika({
id: YM_ID,
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
triggerEvent: true
})
} catch (e) { }
})

const n = d.getElementsByTagName('script')[0]
const s = d.createElement('script')
const f = function () { n.parentNode.insertBefore(s, n) }
s.type = 'text/javascript'
s.async = true
s.src = 'https://mc.yandex.ru/metrika/watch.js'

if (w.opera === '[object Opera]') {
d.addEventListener('DOMContentLoaded', f, false)
} else { f() }
})(document, window, 'yandex_metrika_callbacks')

document.addEventListener('yacounter' + YM_ID + 'inited', function () {
router.afterEach(function (to) {
window['yaCounter' + YM_ID].hit(to.fullPath)
})
})
}

router.onReady(() => {
app.$mount('#app')

Expand Down
1 change: 1 addition & 0 deletions lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ module.exports = function createBaseConfig ({
.use(require('webpack/lib/DefinePlugin'), [{
BASE_URL: JSON.stringify(siteConfig.base || '/'),
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : false,
YM_ID: siteConfig.ym ? JSON.stringify(siteConfig.ym) : false,
SW_ENABLED: !!siteConfig.serviceWorker
}])

Expand Down