tell GA what kind of account it is

fixes KNO-392
flag  = none

the org type that was introduced in g/236404 and g/236535 is now
submitted to GA pageviews as a new dimension (4). These are the current
org types and their account counts, which you can get from Salesforce:

- Corporate                   (1206)
- Further Education           (359)
- Government                  (29)
- Higher Ed                   (2468)
- K12                         (3807)
- Other                       (15)
- RTO/Training                (86)
- RTO                         (1)
- Internal Use Only           (1)
- Regional Training Provider  (1)
- Training Org                (1)

|
| TEST PLAN
|

- make sure you have GA enabled if you don't already:

    Setting.set('google_analytics_key', 'foo')

- visit any page, then look for the GA snippet[1] and verify that
  "dimension4" is set to null

- now mark your account as "K12":

    Account.default.external_integration_keys.create!(
      key_type: 'salesforce_org_type',
      key_value: 'K12'
    )

- reload the page and verify that "dimension4" is set to "K12" in the GA
  snippet

- unmark the account and verify the dimension is back to null:

    Account.default.external_integration_keys.where(
      key_type: 'salesforce_org_type'
    ).destroy_all

[1]: do "view source" in browser then jump to the term "window.ga", or
     you can use this query in the console and inspect the element:

         Array.prototype.find.call(
           document.head.querySelectorAll('script'),
           x => x.innerHTML.match(/window.ga/)
         )

Change-Id: I505bf1ee2a8347a21d691e2fa4ef04cb07c9a48e
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/238656
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Davis Hyer <dhyer@instructure.com>
QA-Review: Davis Hyer <dhyer@instructure.com>
Product-Review: Davis Hyer <dhyer@instructure.com>
This commit is contained in:
Ahmad Amireh 2020-05-28 11:43:19 -06:00
parent 8e11c17578
commit f0284c9f1c
4 changed files with 29 additions and 1 deletions

View File

@ -17,6 +17,7 @@
ga('set', 'dimension1', <%= dimensions.fetch(:enrollments).to_json.html_safe %>);
ga('set', 'dimension2', <%= dimensions.fetch(:admin).to_json.html_safe %>);
ga('set', 'dimension3', <%= dimensions.fetch(:masquerading).to_json.html_safe %>);
ga('set', 'dimension4', <%= dimensions.fetch(:org_type).to_json.html_safe %>);
ga('send', 'pageview');
(window.requestIdleCallback || window.setTimeout)(function(){
var s=document.createElement('script'), m=document.getElementsByTagName('script')[0]

View File

@ -44,6 +44,7 @@ module GoogleAnalyticsDimensions
admin: _encode_admin_status(roles: user_roles),
enrollments: _encode_enrollments(roles: user_roles),
masquerading: _encode_masquerading_status(user: user, real_user: real_user),
org_type: _encode_org_type(account: domain_root_account),
user_id: _compute_non_compromising_user_id(user: user),
}
end
@ -74,6 +75,12 @@ module GoogleAnalyticsDimensions
end.join('')
end
def self._encode_org_type(account:)
account&.external_integration_keys&.find_by(
key_type: 'salesforce_org_type'
)&.key_value
end
def self._encode_masquerading_status(user:, real_user:)
real_user && real_user != user ? '1' : '0'
end

View File

@ -86,7 +86,6 @@ describe GoogleAnalyticsDimensions do
expect(dims[:admin]).to eq '11'
end
it 'tells when someone is masquerading' do
dims = subject[
real_user: account_admin_user,
@ -105,6 +104,15 @@ describe GoogleAnalyticsDimensions do
expect(dims[:masquerading]).to eq '0'
end
it 'reports the org type as found in Salesforce' do
Account.default.external_integration_keys.create!(
key_type: 'salesforce_org_type',
key_value: 'K12'
)
expect(subject[][:org_type]).to eq 'K12'
end
describe 'identification' do
it 'provides a consistent, predictable user identifier' do
user = user_with_pseudonym(active_all: true)

View File

@ -43,6 +43,7 @@ describe "google analytics" do
admin: { key: 'dimension2', default: '00' },
enrollments: { key: 'dimension1', default: '000' },
masquerading: { key: 'dimension3', default: '0' },
org_type: { key: 'dimension4', default: nil }
}
end
@ -133,5 +134,16 @@ describe "google analytics" do
"ga('set', 'userId', #{alternative_user_id.to_json})"
)
end
it "should report the org type as a dimension" do
start_with do
Account.default.external_integration_keys.create!(
key_type: 'salesforce_org_type',
key_value: 'K12'
)
end
expect_dimensions_to_include(org_type: 'K12')
end
end
end