From f6645309a2399c50fc824dd189046f1836962968 Sun, 3 Jun 2018 15:38:53 +0200
From: hass <hass@85918.no-reply.drupal.org>
Date: Sun, 3 Jun 2018 15:38:45 +0200
Subject: [PATCH] Issue #2977078 by hass: Migrate user data from piwik

diff --git a/matomo.module b/matomo.module
index 6622027..380b66b 100644
--- a/matomo.module
+++ b/matomo.module
@@ -401,6 +401,12 @@
       $description .= '</span>';
     }
 
+    // Migrate old piwik settings to matomo.
+    $account_data_piwik = \Drupal::service('user.data')->get('piwik', $account->id());
+    if (isset($account_data_piwik['user_account_users'])) {
+      $account_data_matomo['user_account_users'] = $account_data_piwik['user_account_users'];
+    }
+
     $form['matomo']['user_account_users'] = [
       '#type' => 'checkbox',
       '#title' => t('Enable user tracking'),
@@ -422,6 +428,12 @@
   if ($account->id() && $form_state->hasValue('user_account_users')) {
     \Drupal::service('user.data')->set('matomo', $account->id(), 'user_account_users', (int) $form_state->getValue('user_account_users'));
   }
+
+  // Remove old piwik setting to complete migration to matomo.
+  $account_data_piwik = \Drupal::service('user.data')->get('piwik', $account->id());
+  if (isset($account_data_piwik)) {
+    \Drupal::service('user.data')->delete('piwik', $account->id());
+  }
 }
 
 /**
