export default ({ app: { router } }) => {
  function create() {
    try {
      window['yaCounter<%= options.id %>'] = new Ya.Metrika(<%= JSON.stringify(options) %>)
      app.router.afterEach(to => {
        // We tell Yandex Metrika to add a page view
        window['yaCounter<%= options.id %>'].hit(to.fullPath)
      })
    } catch (e) {
      //
    }
  }

  if (window.Ya && window.Ya.Metrika) {
    create()
  } else {
    (function (w, c) {
      (w[c] = w[c] || []).push(create)
    })(window, 'yandex_metrika_callbacks')
  }
}

@@ -37,16 +37,151 @@
  =%3E %7B%0A  
+let ready = false%0A%0A  router.onReady(() =%3E %7B%0A    // Mark when the router has completed the initial navigation.%0A    ready = true%0A  %7D)%0A%0A  
 function
@@ -193,23 +193,11 @@
 te()
- %7B%0A    try
  %7B%0A
-  
     
@@ -290,14 +290,8 @@
     
-  app.
 rout
@@ -307,18 +307,26 @@
 ach(
+(
 to
+, from)
  =%3E %7B%0A  
     
@@ -325,61 +325,117 @@
 %7B%0A  
+    if (!ready) %7B%0A
       
+  
 // 
-We tell Yandex Metrika to add a page view%0A  
+Don't record a duplicate hit for the initial navigation.%0A        return%0A      %7D%0A
     
@@ -492,51 +492,207 @@
 Path
-)
+, %7B
 %0A      
-%7D)%0A    %7D catch (e) %7B
+  referer: from.fullPath%0A        // TODO: pass title: %3Cnew page title%3E%0A        // This will need special handling because router.afterEach is called *before* DOM is updated.
 %0A      
-//
+%7D)
 %0A    %7D
+)
 %0A  %7D
@@ -741,27 +741,138 @@
     
-create()%0A  %7D else %7B
+// Yandex.Metrika API is already available.%0A    create()%0A  %7D else %7B%0A    // Yandex.Metrika has not loaded yet, register a callback.
 %0A   

fix ReferenceError, fix duplicate initial hit (#147)