title: How can I call addTelemetryInitializer when using the latest Javascript snippet?
body: <p>I am trying to customise the name attribute for pageview events</p>
  <p>This has previously been asked, for example <a href="https://stackoverflow.com/questions/53778932/how-to-provide-custom-names-for-page-view-events-in-azure-app-insights">How to provide custom names for page view events in Azure App Insights?</a></p>
  <p>but this and all other solutions I've found (and the Microsoft documentation too) are working with an old version of the javascript snippet, of the form</p>
  <pre class="lang-js prettyprint-override"><code>window.appInsights = appInsights;
  // …
  appInsights.trackPageView();
  </code></pre>
  <p>The current snippet from the portal is very different though</p>
  <pre class="lang-js prettyprint-override"><code>var sdkInstance=&quot;appInsightsSDK&quot;;window[sdkInstance]=&quot;appInsights&quot;;var // ...
  {
       instrumentationKey:&quot;key&quot;
  }); window[aiName] = aisdk,aisdk.queue &amp;&amp; aisdk.queue.length ===0 &amp;&amp; aisdk.trackPageView({});
  </code></pre>
  <p>I've tried this sort of thing</p>
  <pre class="lang-js prettyprint-override"><code>var sdkInstance=&quot;appInsightsSDK&quot;;window[sdkInstance]=&quot;appInsights&quot;;var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){function n(e){t[e]=function(){var n=arguments;t.queue.push(function(){t[e].apply(t,n)})}}var t={config:e};t.initialize=!0;var i=document,a=window;setTimeout(function(){var n=i.createElement(&quot;script&quot;);n.src=e.url||&quot;https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js&quot;,i.getElementsByTagName(&quot;script&quot;)[0].parentNode.appendChild(n)});try{t.cookie=i.cookie}catch(e){}t.queue=[],t.version=2;for(var r=[&quot;Event&quot;,&quot;PageView&quot;,&quot;Exception&quot;,&quot;Trace&quot;,&quot;DependencyData&quot;,&quot;Metric&quot;,&quot;PageViewPerformance&quot;];r.length;)n(&quot;track&quot;+r.pop());n(&quot;startTrackPage&quot;),n(&quot;stopTrackPage&quot;);var s=&quot;Track&quot;+r[0];if(n(&quot;start&quot;+s),n(&quot;stop&quot;+s),n(&quot;setAuthenticatedUserContext&quot;),n(&quot;clearAuthenticatedUserContext&quot;),n(&quot;flush&quot;),!(!0===e.disableExceptionTracking||e.extensionConfig&amp;&amp;e.extensionConfig.ApplicationInsightsAnalytics&amp;&amp;!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){n(&quot;_&quot;+(r=&quot;onerror&quot;));var o=a[r];a[r]=function(e,n,i,a,s){var c=o&amp;&amp;o(e,n,i,a,s);return!0!==c&amp;&amp;t[&quot;_&quot;+r]({message:e,url:n,lineNumber:i,columnNumber:a,error:s}),c},e.autoExceptionInstrumented=!0}return t}(
  {
    instrumentationKey:&quot;my-key&quot;
  }); window[aiName] = aisdk;
  if (aisdk.queue &amp;&amp; 0 !== aisdk.queue.length) {
    function adjustPageName(item) {
          var name = item.name.replace(&quot;AppName&quot;, &quot;&quot;);
  
          if (name.indexOf(&quot;Order&quot;) !== -1)
              return &quot;Order&quot;;
  
          if (name.indexOf(&quot;Product&quot;) !== -1)
              return &quot;Shop&quot;;
  
          // And so on...
          return name;
      }
  
      // Add telemetry initializer
      aisdk.queue.push(function () {
          aisdk.context.addTelemetryInitializer(function (envelope) {
              var telemetryItem = envelope.data.baseData;
              // To check the telemetry item’s type:
              if (envelope.name === Microsoft.ApplicationInsights.Telemetry.PageView.envelopeType || envelope.name === Microsoft.ApplicationInsights.Telemetry.PageViewPerformance.envelopeType) {
  
                  // Do not track admin pages
                  if (telemetryItem.name.indexOf(&quot;Admin&quot;) !== -1)
                      return false;
  
                  telemetryItem.name = adjustPageName(telemetryItem);
              }
  
          });
      });
    aisdk.trackPageView();
  };
  </code></pre>
  <p>But it doesn't work (no errors, but no effect on the telemetry either)</p>
  <p>Has anyone managed to get anything like this working using the new snippet?</p>