Repository: akvo/akvo-rsr

Issue:
Akvoapp pages missing Piwik web analytic ids
When a new akvoapp partner/partner page is added someone needs to request a Piwikid.  This has not been done for a number of akvoapp.org pages.

As per the attached, please update the piwik tracking variable in the RSR database.


[rsr_partnersite_piwik_missing_ids.txt](https://github.com/akvo/akvo-rsr/files/656973/rsr_partnersite_piwik_missing_ids.txt)

In future could RSR prompt the user adding the new akvoapp.org page to request/require a Piwikid?



Discussion:
The user adding a new akvoapp.org page is always someone from Akvo, right? @Lynngre 
Also, from where/how is this done, usually? 
@nadiagorchakova as far as I understand, this is created in the backend by an Akvonaut, yes.

@punchagan previously a developer has uploaded the Piwikid's into the database. The javascript tracking code is inserted into the footer of rsr.akvo.org & loads a variable depending on which akvoapp.org page is loaded (so standard rsr.akvo.org is idsite '1', for example.  

<script type="text/javascript">
  var pkBaseURL = "https://analytics.akvo.org/";
  document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  try {
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
  } catch( err ) {}
</script>
<noscript>
  <p>
    <img src="https://analytics.akvo.org/piwik.php?idsite=1" style="border:0" alt="" />
  </p>
</noscript>

<!-- End Piwik Tracking Code -->


This changes to idsite '44' for example if washalliance.akvoapp.org is loaded:

<script type="text/javascript">
  var pkBaseURL = "https://analytics.akvo.org/";
  document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  try {
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 44);
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
  } catch( err ) {}
</script>
<noscript>
  <p>
    <img src="https://analytics.akvo.org/piwik.php?idsite=44" style="border:0" alt="" />
  </p>
</noscript>

<!-- End Piwik Tracking Code -->

The relevant idsite needs to be uploaded into the relevant RSR table.  

The Piwik ids are created on analytics.akvo.org when you add a new site to track via the admin.




Resolving patch:
diff --git a/akvo/rsr/migrations/0092_auto_20170216_1209.py b/akvo/rsr/migrations/0092_auto_20170216_1209.py
new file mode 100644
--- /dev/null
+++ b/akvo/rsr/migrations/0092_auto_20170216_1209.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations, transaction
+
+# rsr_partnersite id,Name,Piwikid
+MISSING_DATA = """\
+151,afdb,118
+122,akvoeastafrica,119
+127,akvoflow,120
+136,akvoppp3,121
+125,akvowestafrica,123
+138,drydev,124
+154,eutf,125
+121,iccoasia,126
+135,iccoindia,127
+106,iggwater,128
+142,kepa,129
+147,leprosyrelief,130
+123,nlembassykenya,131
+150,nlembassymozambique,132
+152,nso-g4aw,133
+126,pind,134
+130,planfinland,135
+153,snvworld,136
+149,vnginternational,137
+134,walkingforwater2016,138
+144,walkingforwater2017,139
+133,wandelenvoorwater2016,140
+143,wandelenvoorwater2017,141
+131,washmali,142
+145,worldveg,143
+103,yepprogrammes,144
+"""
+
+
+def assign_piwik_ids(apps, schema_editor):
+    PartnerSite = apps.get_model('rsr', 'PartnerSite')
+
+    with transaction.atomic():
+        for line in MISSING_DATA.strip().splitlines():
+            partner_site_id, hostname, piwik_id = line.split(',')
+            try:
+                partner_site = PartnerSite.objects.get(id=partner_site_id)
+            except PartnerSite.DoesNotExist:
+                pass
+            else:
+                if not partner_site.hostname == hostname:
+                    print "{0}'s host name is '{1}', expected '{2}'. Skipping!".format(
+                        partner_site_id, partner_site.hostname, hostname
+                    )
+                    continue
+
+                partner_site.piwik_id = piwik_id
+                partner_site.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('rsr', '0091_auto_20170208_1035'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            assign_piwik_ids,
+            # Required to not crash when trying to run a backwards migration
+            reverse_code=lambda x, y: None
+        )
+    ]


Test patch: